react组件状态

2020-11-04  本文已影响0人  胡齐峰

事件

class Cmp extends React.Component{
    constructor(...args){
        super(...args)
        this.a = 12
    }
    fn(){
        this.a = this.a+1;
    }
    render(){
        return (
            <div>
                <span>{this.a}</span>
                <input type="button" value="按钮" onClick={this.fn.bind(this)} />
            </div>
        )
    }
}
ReactDOM.render(<Cmp></Cmp>,document.getElementById('root'))

状态设置与改变

class Cmp extends React.Component{
    constructor(...args){
        super(...args)
        this.state = {
            a:12
        }
    }
    fn(){
        //只改变state中的值但不会触发渲染
        this.state.a = this.state.a+1;
        console.log(this.state.a)
    }
    fn2(){
        //状态的设置必须要使用setState,否则不会触发渲染
        this.setState({
            a:this.state.a+1
        })
        console.log(this.state.a)
    }
    
    render(){
        //input可以使用单标签,此处是为了markdown代码高亮
        return (
            <div>
                <span>{this.state.a}</span>
                <input type="button" value="按钮1" onClick={this.fn.bind(this)}></input>
                <input type="button" value="按钮2" onClick={this.fn2.bind(this)} ></input>
            </div>
        )
    }
}
ReactDOM.render(<Cmp></Cmp>,document.getElementById('root'))

props变化引起重新渲染

Uncaught TypeError: Cannot assign to read only property 'a' of object '#<Object>'

class Cmp extends React.Component{
    constructor(...args){
        super(...args)
    }
    fn(){
        this.props.a = this.props.a+1;
    }
    render(){
        return (
            <div>
                <span>{this.props.a}</span>
                <input type="button" value="按钮1" onClick={this.fn.bind(this)} ></input>
            </div>
        )
    }
}
class Parent extends React.Component{
    constructor(...args){
        super(...args)
        this.state={
            text:12
        }
    }
    fn(){
        this.setState({
            text:this.state.text + 1
        })
    }
    render(){
        return (
            <div>
                <Child text={this.state.text}></Child>
                <input type="button" value="按钮1" onClick={this.fn.bind(this)} ></input>
            </div>
        )
    }
}
class Child extends React.Component{
    constructor(...args){
        super(...args)
    }
    render(){
        return (
            <div>{this.props.text}</div>
        )
    }
}
ReactDOM.render(<Parent></Parent>,document.getElementById('root'))

react触发渲染的三种情况

  1. state使用setStates时;
  2. props传值发生改变时;
  3. 强制渲染调用this.forceUpdate()
class Cmp extends React.Component{
    constructor(...args){
        super(...args)
        this.a = 12
    }
    fn(){
        this.a = this.a+1;
        //强烈不建议使用,会严重的影响性能以及一些不可预知的错误。
        this.forceUpdate()
    }
    render(){
        return (
            <div>
                <span>{this.a}</span>
                <input type="button" value="按钮1" onClick={this.fn.bind(this)} ></input>
            </div>
        )
    }
}
ReactDOM.render(<Cmp a = {12}></Cmp>,document.getElementById('root'))
上一篇 下一篇

猜你喜欢

热点阅读