ReactJS state vs prop

后端 未结 6 1027
予麋鹿
予麋鹿 2021-01-29 17:59

This may be treading that line between answerable and opinionated, but I\'m going back and forth as to how to structure a ReactJS component as complexity grows and could use som

6条回答
  •  终归单人心
    2021-01-29 18:35

    I'm not sure if I'm answering your question, but I've found that, especially in a large/growing application, the Container/Component pattern works incredibly well.

    Essentially you have two React components:

    • a "pure" display component, which deals with styling and DOM interaction;
    • a container component, which deals with accessing/saving external data, managing state, and rendering the display component.

    Example

    N.B. This example is a probably too simple to illustrate the benefits of this pattern, as it is quite verbose for such a straightforward case.

    /**
     * Container Component
     *
     *  - Manages component state
     *  - Does plumbing of data fetching/saving
     */
    
    var PostEditorContainer = React.createClass({
      getInitialState: function() {
        return {
          text: ""
        };
      },
    
      componentWillMount: function() {
        this.setState({
          text: getPostText()
        });
      },
    
      updateText: function(text) {
        this.setState({
          text: text
        });
      },
    
      savePost: function() {
        savePostText(this.state.text);
      },
    
      render: function() {
        return (
          
        );
      }
    });
    
    
    /**
     * Pure Display Component
     *
     *  - Calculates styling based on passed properties
     *  - Often just a render method
     *  - Uses methods passed in from container to announce changes
     */
    
    var PostEditor = React.createClass({
      render: function() {
        return (
          
    ); } });

    Benefits

    By keeping display logic and data/state management separate, you have a re-usable display component which:

    • can easily be iterated with different sets of props using something like react-component-playground
    • can be wrapped with a different container for different behavior (or combine with other components to build larger parts of your application

    You also have a container component which deals with all external communication. This should make it easier to be flexible about the way you access your data if you make any serious changes later on*.

    This pattern also makes writing and implementing unit tests a lot more straightforward.

    Having iterated a large React app a few times, I've found that this pattern keeps things relatively painless, especially when you have larger components with calculated styles or complicated DOM interactions.

    *Read up on the flux pattern, and take a look at Marty.js, which largely inspired this answer (and I have been using a lot lately) Redux (and react-redux), which implement this pattern extremely well.

    Note for those reading this in 2018 or later:

    React has evolved quite a bit since this answer was written, especially with the introduction of Hooks. However, the underlying state management logic from this example remains the same, and more importantly, the benefits that you get from keeping your state and presentation logic separate still apply in the same ways.

提交回复
热议问题