JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

React Tutorial: Props Anti-Pattern

Author:JIYIK Last Updated:2025/03/15 Views:

In the article Passing Props , we learned about the concept of React Props and how to use it. In this article, we will introduce the use of Props to generate state in getInitialState. This method of use is called Anti-Pattern.

The reason why it is called an anti-pattern is that using Props to generate state in getInitialState will cause repeated "source trust" problems. In addition, the getInitialState function is only called when the component is rendered for the first time. That is to say, when the component is rendered for the second time, the getInitialState function will not be called again. That is to say, if we want to change the value of Props and thus the value of State through the second rendering, it is impossible.

Let's look at a piece of code:

var MessageBox = React.createClass({
    getInitialState: function() {
        return {name: 'Mr. ' + this.props.name};
    },
    render: function() {
        return <div>{'Mr. '+this.state.name}</div>;
    }
});
ReactDOM.render(
    <MessageBox name="Rogers" />,
    document.getElementById('content')
);

The above example is an anti-pattern. If we want to render the MessageBox component again and change the name value of Props, we will find that the content in the div remains unchanged, and it is still Mr. Rogers instead of Mr. Onmpw.

ReactDOM.render(
    <MessageBox name="Rogers" />,
    document.getElementById('content')
);
ReactDOM.render(
    <MessageBox name="Onmpw" />,
    document.getElementById('content')
);

So, for the above example, it is better to use this.props directly in div rather than this.state.

var MessageBox = React.createClass({
    render: function() {
        return <div>{'Mr. '+this.props.name}</div>;
    }
});
ReactDOM.render(
    <MessageBox name="Onmpw" />,
    document.getElementById('content')
);

The result of this example is Mr. Onmpw output on the page.

ReactDOM.render(
    <MessageBox name="Rogers" />,
    document.getElementById('content')
);

If we render again, it will be Mr. Rogers.

The situation described above is an anti-pattern of using Props in getInitialState. Of course, if we can clearly define the purpose of Props , using it in getInitialState will not produce anti-patterns. For example, when a value is initialized, the value of the Prop will not change.

var Counter = React.createClass({
    getInitialState: function() {
        return {count: this.props.initialCount};
    },
    handleClick: function() {
        this.setState({count: this.state.count + 1});
    },
    render: function() {
        return <div onClick={this.handleClick}>{this.state.count}</div>;
    }
});
ReactDOM.render(
    <Counter initialCount={7}/>,
    document.getElementById('content')
);

The above examples avoid the anti-pattern. The above examples are modified from the examples in the official documentation. I hope this article will help you learn React .

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to avoid cross-origin (CORS) issues in React/Next.js

Publish Date:2025/03/17 Views:170 Category:NETWORK

In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:188 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:102 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:60 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:191 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:90 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial