MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Common Antipattern


You should not save props into state. It is considered an anti-pattern. For example:

export default class MyComponent extends React.Component {
 constructor() {
 super();
 this.state = {
 url: ''
 }
 this.onChange = this.onChange.bind(this);
 }
 onChange(e) {
 this.setState({
 url: this.props.url + '/days=?' + e.target.value
 });
 }
 componentWillMount() {
 this.setState({url: this.props.url});
 }
 render() {
 return (
 <div>
 <input defaultValue={2} onChange={this.onChange} />
URL: {this.state.url}
 </div>
 )
 }
}

The prop url is saved on state and then modified. Instead, choose to save the changes to a state, and then build
the full path using both state and props:

export default class MyComponent extends React.Component {
 constructor() {
 super();
 this.state = {
 days: ''
 }
 this.onChange = this.onChange.bind(this);
 }
 onChange(e) {
 this.setState({
 days: e.target.value
 });
 }
 render() {
 return (
 <div>
 <input defaultValue={2} onChange={this.onChange} />
 URL: {this.props.url + '/days?=' + this.state.days}
 </div>
 )
 }
}

This is because in a React application we want to have a single source of truth - i.e. all data is the responsibility of one single component, and only one component. It is the responsibility of this component to store the data within its state, and distribute the data to other components via props.

In the first example, both the MyComponent class and its parent are maintaining 'url' within their state. If we update state.url in MyComponent, these changes are not reflected in the parent. We have lost our single source of truth, and it becomes increasingly difficult to track the flow of data through our application. Contrast this with the second example - url is only maintained in the state of the parent component, and utilised as a prop in MyComponent - we therefore maintain a single source of truth.

Conclusion

In this page (written and validated by ) you learned about React Common Antipattern . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React setState.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.