setState doesn't update the state immediately

前端 未结 12 2482
暖寄归人
暖寄归人 2020-11-21 05:25

I would like to ask why my state is not changing when I do an onclick event. I\'ve search a while ago that I need to bind the onclick function in constructor but still the s

相关标签:
12条回答
  • 2020-11-21 05:40

    setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.

    setState() will always lead to a re-render unless shouldComponentUpdate() returns false. If mutable objects are being used and conditional rendering logic cannot be implemented in shouldComponentUpdate(), calling setState() only when the new state differs from the previous state will avoid unnecessary re-renders.

    The first argument is an updater function with the signature:

    (state, props) => stateChange
    

    state is a reference to the component state at the time the change is being applied. It should not be directly mutated. Instead, changes should be represented by building a new object based on the input from state and props. For instance, suppose we wanted to increment a value in state by props.step:

    this.setState((state, props) => {
        return {counter: state.counter + props.step};
    });
    
    0 讨论(0)
  • 2020-11-21 05:41

    Fortunately setState() takes a callback. And this is where we get updated state.

    Consider this example.

    this.setState({ name: "myname" }, () => {                              
            //callback
            console.log(this.state.name) // myname
          });
    

    So When callback fires, this.state is the updated state.
    You can get mutated/updated data in callback.

    0 讨论(0)
  • 2020-11-21 05:43

    Since setSatate is a asynchronous function so you need to console the state as a callback like this.

    openAddBoardModal(){
        this.setState({ boardAddModalShow: true }, () => {
            console.log(this.state.boardAddModalShow)
        });
    }
    
    0 讨论(0)
  • 2020-11-21 05:44

    setState() is asynchronous. The best way to verify if the state is updating would be in the componentDidUpdate() and not to put a console.log(this.state.boardAddModalShow) after this.setState({ boardAddModalShow: true }) .

    according to React Docs

    Think of setState() as a request rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately

    0 讨论(0)
  • 2020-11-21 05:45

    when i was running the code and checking my output at console it showing the that it is undefined. After i search around and find something that worked for me.

    componentDidUpdate(){}
    

    I added this method in my code after constructor(). check out the life cycle of react native workflow.

    https://images.app.goo.gl/BVRAi4ea2P4LchqJ8

    0 讨论(0)
  • 2020-11-21 05:45

    Yes because setState is an asynchronous function. The best way to set state right after you write set state is by using Object.assign like this: For eg you want to set a property isValid to true, do it like this


    Object.assign(this.state, { isValid: true })


    You can access updated state just after writing this line.

    0 讨论(0)
提交回复
热议问题