【问题标题】:Parent passes state to child - React js父母将状态传递给孩子 - React js
【发布时间】:2019-05-07 12:41:26
【问题描述】:

ObjectList 有一个以列表形式呈现的对象数组。当用户单击列表项时,该对象将被发送回 ObjectEditor,以便用户可以查看它并继续编辑。问题是我不确定如何将该对象传递给 ObjectEditor,因为单击事件发生在 ObjectList 中。

我最初的解决方案是将它作为道具传递给 ObjectEditor 并使用 componentWillReceiveProps 方法来更新 ObjectEditor 状态。但是,该解决方案并不实用,因为我不希望它在每次道具更改时都更新。有没有更好的办法?

我是 React 的新手,所以在我了解 React 之前,我想暂时避免使用 Redux。

为了清晰起见,我已经大量削减了代码。

对象列表:

      constructor(props){
       super(props);
       this.state = { objects: [
          {title: '', items: [], anotherThing:''},
          {title: '', items: [], anotherThing:''}
        ]}
      }

      viewObject = (index) => {
      let object = {...this.state.object[index]};
     // Then some code that passes the object to the ObjectEditor Component
       }

      render(){
        return(
          <div>

           <li key={index} onClick={ () => this.viewObject(index) } >
           // A list of titles from state
           </li>

          <ObjectEditor />
          </div>
        )
      }

对象编辑器:

       constructor(props){
       super(props);
       this.state = {title:'', items: [], anotherThing:''}
       }

    // various event handlers that update the state based off form inputs

      render(){
        return(
          <div> 

            // Various form fields which get pushed to state

            <button>Save & Add New</button> 

            // function that maps through state and renders it to the page
          </div>
        )
      }
    }

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    我的建议是让父组件处理所有状态和逻辑,并将ObjectEditor 组件保持为一个简单的表示组件,没有自己的逻辑或状态。它看起来有点像这样。

    class Parent extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                objects: [
                    { title: '', items: [], anotherThing: '' },
                    { title: '', items: [], anotherThing: '' }
                ],
                editObject: {},
            }
        }
    
        viewObject = (index) => {
            let object = { ...this.state.object[index] };
            this.setState({editObject: object}); // sets the state if the clicked item.
            // Then some code that passes the object to the ObjectEditor Component
        }
    
        handleChange = (e) => {
            // handle change
        }
    
        render() {
            return (
                <div>
    
                    <li key={index} onClick={() => this.viewObject(index)} >
                    // A list of titles from state
                    </li>
    
                    <ObjectEditor viewObject={this.state.viewObject} handleChange={this.handleChange} />
                </div>
            )
        }
    }
    
    
    class ObjectEditor extends React.Component {
        render() {
            return (
                // render some sort of editor
                // display data based on the props passed down
                // when user edits in the form, call up to the parent's change handler
            );
        }
    }
    

    【讨论】:

    • 不幸的是,ObjectEditor 必须有状态,因为它使用允许用户重新排序项目的拖放库。也许是时候看看 Redux 了。
    • 你的父母不能处理这个逻辑吗?
    • 你的问题给了我一个想法!我向 ObjectLists 状态添加了一个“currentItem”值,该值在单击项目时发生更改,然后我将其作为道具传递给 ObjectEditor 并使用 componentWillReceiveProps。谢谢!
    猜你喜欢
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2018-08-04
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    相关资源
    最近更新 更多