GithubHelp home page GithubHelp logo

Comments (2)

NiGhTTraX avatar NiGhTTraX commented on July 3, 2024 1

Even more, here it's stated that in the majority of simple cases having props in getInitialState is an anti-pattern.

It's an anti-pattern because if you set initial state using props, then you might have to resync it when new props come in. This is an indication that the props are the source of truth and that your component shouldn't hold state.

class C extends React.Component {
  constructor(props) {
    this.state = {
      foo: this.props.foo
    };
  }
  render() {
    return <div>{this.state.foo}</div>;
  }
}

ReactDOM.render(<C foo="bar" />, container);
ReactDOM.render(<C foo="baz" />, container);

The problem with the above is that you will probably expect "baz" to be rendered, but instead you'll get "bar" because that's the current state of the component and it wasn't updated when new props came in.

To fix that you can implement the componentWillReceiveProps method:

class C extends React.Component {
  constructor(props) {
    this.state = {
      foo: this.props.foo
    };
  }
  render() {
    return <div>{this.state.foo}</div>;
  }
  componentWillReceiveProps(nextProps) {
    // This is a code smell. It's telling you that the props are more
    // important than the state and that you should lose one of them.
    this.setState({ foo: nextProps.foo });
  }
}

Did that answer your question?

from react-guide.

demns avatar demns commented on July 3, 2024

@NiGhTTraX Andrei, thank you for your answer. What I actually meant is that you cannot automatically assign state value in the child component from the parent, the thing that you can do with props (without additional steps, of course). It was a bit confusing for me, but now I can understand that having yes in the table doesn't necessarily mean that it is done by React :)

from react-guide.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.