简体   繁体   English

将数据从子组件传递到父组件

[英]Passing data from child to parent component

I was reading a tutorial on Medium that explained how to pass data from a child component to parent ( https://medium.com/@ruthmpardee/passing-data-between-react-components-103ad82ebd17 ). 我正在阅读有关Medium的教程,该教程解释了如何将数据从子组件传递到父组件( https://medium.com/@ruthmpardee/passing-data-between-react-components-103ad82ebd17 )。 The tutorial got 5.5k likes, which means many people must've used it as a reference in their own work. 该教程获得了5.5k点赞,这意味着很多人必须在自己的工作中使用它作为参考。 However, upon replicating the code 1:1, I was completely unable to to get the same results. 但是,在复制代码1:1时,我完全无法获得相同的结果。 In fact, data wasn't being passed from the child to the parent at all. 实际上,数据根本没有从孩子传递给父母。 Plus, when I forced data to pass up, I got an infinite loop. 另外,当我强制数据通过时,我遇到了无限循环。 I would greatly appreciate if anyone can point out where I am wrong, or if in fact, it is the tutorial after all. 如果有人指出我错了,或者实际上毕竟是本教程,我将不胜感激。

JS fiddle to my code: https://jsfiddle.net/lightspeed12/69z2wepo/216279/ JS拨弄我的代码: https : //jsfiddle.net/lightspeed12/69z2wepo/216279/

 class ToDoItem extends React.Component { someFn = () => { let listInfo = 'Hi mom' this.props.callBackFromParent(listInfo); } render(){return <h3>Hello World</h3>} }; class ToDoList extends React.Component { constructor(props){ super(props) this.state = { listDataFromChild: null } } myCallback = (dataFromChild) => { this.setState({ listDataFromChild : dataFromChild }) } otherFn = () => { console.log(this.state.listDataFromChild, 'from state') } render(){ this.otherFn(); //calling otherFn to determine value of this.state.listDataFromChild return ( <div> <h2>Message from Child is:</h2> <ToDoItem callBackFromParent={this.myCallback} /> </div> ) } } ReactDOM.render( <ToDoList />, document.getElementById('container') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

this.someFn() of ToDoItem is never executed. this.someFn()ToDoItem从不执行。

Try: 尝试:

// To-do Item.
class ToDoItem extends React.Component {

  // Render.
  render = () => <h3>Hello World</h3>

  // Did Mount.
  componentDidMount() {
    this.props.callBackFromParent('Callback Received.')
  }

}

See below for a practical example of a functional prop being passed and executed . 有关passedexecuted functional prop的实际示例,请参见下文。

 // Parent. class Parent extends React.Component { // State. state = {data: null} // Callback. callback = data => this.setState({data}) // Render. render(){ const {data} = this.state console.log('Data:', data) return ( <React.Fragment> <h2>Parent</h2> <Child callback={this.callback}/> Data: {data || 'null'} </React.Fragment> ) } } // Child. const Child = props => <button onClick={() => props.callback('Callback Received.')}>Child.</button> // Mount. ReactDOM.render(<Parent/>,document.querySelector('#root')) 
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root"></div> 

this.someFn() of ToDoItem is never called ,which calls the callback method to ToDoList component. 永远不会调用this.someFn() ,它会调用ToDoList组件的回调方法。 Here this.someFn() is called on a button click. 在此单击按钮时将调用this.someFn()

    class ToDoItem extends React.Component {
      someFn = () => {
        let listInfo = 'Hi mom'
        this.props.callBackFromParent(listInfo);
      }
       componentDidMount() {
          this.someFn()
  }
  render(){return <h3>Hello World</h3>}
};

Data from child can be viewed as 来自儿童的数据可以视为

  render(){
        return (
          <div>
            <h2>Message from Child is:{this.state.listDataFromChild}</h2>

            <ToDoItem 
              callBackFromParent={this.myCallback}
            />  
          </div>
        )
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM