Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

functional setState and props

I know well why we need functional setState and how it works, e.g.

this.setState((prevState, props) => ...);

You get the previous state as a parameter as above.

However pay attention to props too in the arguments. Here I encountered explanation about the props in functional setState:

In addition, it also applies when the update depends on props. These can become stale as well when the received props from the parent component have changed before the asynchronous execution kicks. Therefore, the function in this.setState() gets as second argument the props.

However that explanation still doesn't click to me.

Can someone bring an example how the "props" could become stale? e.g. maybe a code snippet which demonstrates a bug when using this.props instead of "props" as specified in the arguments of the callback function which setState takes?

In other words I don't get why there is need for props argument in functional setState and would be nice to see an example why it is needed.

like image 952
davidm Avatar asked Jul 11 '18 19:07

davidm


People also ask

Can I use setState in functional component?

Yes, but you might not want to. With class components we would keep all of our state in one object. Then when we changed state with setState , we would provide an object and the API would "merge" its contents into the existing state for us. This doesn't happen with setting state in function components with useState .

What is setState function?

Before updating the value of the state, we need to build an initial state setup. Once we are done with it, we use the setState() method to change the state object. It ensures that the component has been updated and calls for re-rendering of the component.

Can functional components have props?

Functional components can accept and use props. Functional components should be favored if you do not need to make use of React state.

What is difference between state and props?

Props are used to pass data from one component to another. The state is a local data storage that is local to the component only and cannot be passed to other components.


1 Answers

class Children extends React.Component {
  state = {
    initial: true,
  };

  onClick = () => {
    this.props.handler();
    console.log(this.state.initial, this.props.initial); // true true
    this.setState((state, props) => {
      console.log(state.initial, props.initial); // true false
      console.log(this.state.initial, this.props.initial); // true true
    });
  };
  render() {
    console.log("render children", this.state.initial, this.props.initial);
    return <div onClick={this.onClick}>Click me</div>
  }
}

class Hello extends React.Component {
  state = {
    initial: true,
  };

  handler = () => {
    this.setState({initial: false});
  }
  render() {
    console.log("render parent", this.state.initial);
    return <Children initial={this.state.initial} handler={this.handler} />
  }
}

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);

In this example, the children will call the handler which will change the parent state and update his own state. In the callback, you can see that props and this.props are different: props are the new values while this.props are stale.

Demo here

like image 163
Axnyff Avatar answered Sep 24 '22 10:09

Axnyff