Comments (2)
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.
@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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-guide.