React state 基础使用

import React, { Component } from 'react'

class index extends Component {
constructor(props) {

 super(props)
 this.state = {
   count:10,
   name:'小刘',
   obj: {
     a: 1,
     b: 2,
     c:3,
   } 
 }

}

setName() {

//  const { name } = this.state;
 this.setState({
   name:'老刘'
 })

}

setA() {

 //  const { obj } = this.state;
   const obj = Object.assign({},this.state.obj, { a:10})
 setTimeout(() => {
   this.setState({
    obj:obj
   })
  },1000)
}

setB() {

 const b={b:20}
 const obj = {
   ...this.state.obj,
   ...b
 }
 setTimeout(() => {
   this.setState({
   obj:obj
  })
 },2000)
 

}
componentDidMount() {

}
render() {

 const { name ,obj ,count} = this.state;
 
return (
  
{name}
{count}
Obj a的值是{obj.a}
Obj b的值是{obj.b}
Obj c的值是{obj.c}
)

}
}

export default index

你可能感兴趣的:(react.js)