简体   繁体   English

反应setState重新渲染

[英]React setState re-render

First of all, I'm really new into React, so forgive my lack of knowledge about the subject. 首先,我真的是React的新手,所以请原谅我对主题的知识不足。

As far as I know, when you setState a new value, it renders again the view (or parts of it that needs re-render). 据我所知,当setState一个新值时,它将再次渲染视图(或需要重新渲染的部分视图)。

I've got something like this, and I would like to know if it's a good practice or not, how could I solve this kind of issues to improve, etc. 我有这样的事情,我想知道这是否是一个好习惯,如何解决此类问题以进行改进等。

class MyComponent extends Component {
    constructor(props) {
        super(props)
        this.state = { 
            key: value
        }
        this.functionRender = this.functionRender.bind(this)
        this.changeValue = this.changeValue.bind(this)
    }
    functionRender = () => {
        if(someParams !== null) {
            return <AnotherComponent param={this.state.key} />
        }
        else {
            return "<span>Loading</span>"
        }
    }
    changeValue = (newValue) => {
        this.setState({
            key: newValue
        })
    }
    render() {
        return (<div>... {this.functionRender()} ... <span onClick={() => this.changeValue(otherValue)}>Click me</span></div>)
    }
}

Another component 另一个组成部分

class AnotherComponent extends Component {
    constructor (props) {
        super(props)
    }
    render () {
        return (
            if (this.props.param === someOptions) {
                return <div>Options 1</div>
            } else {
                return <div>Options 2</div>
            }
        )
    }
}

The intention of the code is that when I click on the span it will change the key of the state, and then the component <AnotherComponent /> should change because of its parameter. 该代码的目的是,当我单击范围时,它将更改状态的键,然后由于其参数,组件<AnotherComponent />应该更改。

I assured that when I make the setState, on the callback I throw a console log with the new value, and it's setted correctly, but the AnotherComponent doesn't updates, because depending on the param given it shows one thing or another. 我保证当我设置setState时,在回调函数上我会抛出一个带有新值的控制台日志,并且它的设置正确,但是AnotherComponent不会更新,因为根据给定的参数它会显示一件事或另一件事。

Maybe I need to use some lifecycle of the MyComponent? 也许我需要使用MyComponent的某些生命周期?

Edit 编辑

I found that the param that AnotherComponent is receiving it does not changes, it's always the same one. 我发现AnotherComponent接收到的param没有改变,它始终是相同的。

I would suggest that you'll first test it in the parent using a simple console.log on your changeValue function: 我建议您首先在changeValue函数上使用一个简单的console.log在父级中对其进行测试:

changeValue = (newValue) => {
    console.log('newValue before', newValue);
    this.setState({
        key: newValue
    }, ()=> console.log('newValue after', this.state.key))
}

setState can accept a callback that will be invoked after the state actually changed (remember that setState is async) . setState可以接受将在状态实际更改后调用的回调 (请记住setState 是async)

Since we can't see the entire component it's hard to understand what actually goes on there. 因为我们看不到整个组件,所以很难理解那里到底发生了什么。 I suspect that the newValue parameter is always the same but i can't be sure. 我怀疑newValue参数始终是相同的,但我不确定。
It seems like you're missing the props in AnotherComponent 's constructor. 好像您在AnotherComponent的构造函数中缺少了props it should be: 它应该是:

 constructor (props) {
    super(props) // here
}

Try replacing the if statement with: 尝试将if语句替换为:

{this.props.param === someOptions? <div>Options 1</div>: <div>Options 2</div>}

also add this function to see if the new props actually get to the component: 还添加此函数以查看新道具是否真正到达组件:

componentWillReceiveProps(newProps){
    console.log(newProps);
}

and check for the type of param and someOptions since you're (rightfully) using the === comparison. 并检查paramsomeOptions的类型,因为(正确地)使用了===比较。

First, fat arrow ( => ) autobind methods so you do not need to bind it in the constructor, second re-renders occur if you change the key of the component. 首先,胖箭头( => )自动绑定方法,因此您无需在构造函数中将其绑定,如果更改组件的键,则第二次重新渲染。

Ref: https://reactjs.org/docs/lists-and-keys.html#keys 参考: https : //reactjs.org/docs/lists-and-keys.html#keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM