Reactjs - How to pass values from child component to grand-parent component?

前端 未结 2 1077
孤城傲影
孤城傲影 2020-12-31 16:00

Below is correct example for passing the values from child component to parent component in reactjs.

App.jsx

import React from \'react         


        
2条回答
  •  清酒与你
    2020-12-31 16:33

    You can pass the update function to the grand child by props, just pass it again from the child component.

    class App extends React.Component {
      constructor(props) {
        super(props)
        this.state = {
          data: 'Initial data...'
        }
        this.updateState = this.updateState.bind(this);
      }
    
      updateState(who) {
        this.setState({data: `Data updated from ${who}`})
      }
    
      render() {
        return (
          
    Parent: {this.state.data}
    ) } } class Child extends React.Component { render() { return (
    Child component
    ); } } class GrandChild extends React.Component { render() { return (
    Grand child component
    ); } } ReactDOM.render(, document.getElementById('root'))
    
    
    

提交回复
热议问题