【问题标题】:How to pass props from child component to its parent of parent?如何将道具从子组件传递到其父组件的父组件?
【发布时间】:2020-07-30 12:35:06
【问题描述】:

假设我有三个组件,其中 SubMain2 在 SubMain1 内,而 SubMain1 在 Main 内。如何将道具从 SubMain2 传递到 Main 组件?

问题:TypeError:无法读取未定义的属性“handleSubMain1”

主要

class Main extends Component {
  constructor(props) {
  super(props);
  this.handleSubMain1 = this.handleSubMain1.bind(this);
  }

 handleSubMain1() {
  console.log('received props from SubMain1 which is from SubMain2!');
 }

  render() {
     <SubMain1 handleSubMain1={this.handleSubMain1}/>
  }
}

SubMain1

class SubMain1 extends Component {

 constructor(props) {
 super(props);
 this.handleSubMain2 = this.handleSubMain2.bind(this);
 }

 handleSubMain2() {
  console.log('received props from SubMain2!');
 }

  render() {
     <SubMain2 handleSubMain2={this.handleSubMain2}/>
  }
}

SubMain2

Component SubMain2 extends Component {
   constructor(props) {
   super(props);
   }

   render() {
     this.props.handleSubMain2();
  }
}

我尝试了这种方法,但是我发现 handleSubMain1 函数未定义。我什至尝试将道具从 SubMain2 直接传递给 Main 组件,但是没有运气。

【问题讨论】:

  • 实际上,在所有组件中,SubMain2 中的 JSX 无效。组件中传递的道具不是this.XX。 SubMain1 没有对 handleSubMain1 Main 做任何事情,试图通过它。 SubMain2 与从 SubMain1 传递的回调相同。
  • 道歉,修复了关于 JSX 的问题。
  • SubMain1 正在从 SubMain2 接收道具,问题是我无法将道具从 SubMain1 传递到 Main。或者有没有办法将道具从 SubMain2 直接传递到 Main?

标签: reactjs react-props react-component


【解决方案1】:

这是我制作的沙盒,您可以查看控制台日志:https://codesandbox.io/s/funny-chebyshev-n5srl?file=/src/App.js:63-717

这里是你需要的代码:


class Main extends Component {
  handleSubMain1(arg) {
    console.log('received props from SubMain1 which is from SubMain2!');
    console.log(arg);
 }

  render() {
     return <SubMain1 handleSubMain1={this.handleSubMain1}/> 
  }
}

class SubMain1 extends Component {
  render() {
     return <SubMain2 handleSubMain2={this.props.handleSubMain1}/>
  }
}

class SubMain2 extends Component {
  handleClick(){
    this.props.handleSubMain2('any thing I pass as an argument I can access it in main');
  }

  render() {
     return (
       <button onClick={this.handleClick.bind(this)}>
         Click me to send data to Main
       </button>
     )
 }
}

【讨论】:

  • 如何将道具从 SubMain2 传递到 Main?这是我的问题
  • 子 -> 父 -> 父?
  • 你是绝对正确的我误解了这个问题,我不知道你为什么要这样做,但我编辑这个答案向你展示@jyoo
  • 我明白了。我忘了在其中一个组件中绑定函数...感谢您的帮助!
  • 我在我的问题中写了它,但不是在我的代码中..哈哈
【解决方案2】:

道具只会向下传递给孩子。下一行的每个子组件都需要调用传递给它的回调。

class Main extends Component {
  mainCallback = val => console.log("mainCallback", val);

  render() {
    return <SubMain1 handleSubMain1={this.mainCallback} />;
  }
}

class SubMain1 extends Component {
  subMain1Callback = val => {
    console.log("subMain1Callback");
    this.props.handleSubMain1(val);
  };

  render() {
    return <SubMain2 handleSubMain2={this.subMain1Callback} />;
  }
}

class SubMain2 extends Component {
  subMain2Callback = val => {
    console.log("subMain2Callback");
    this.props.handleSubMain2(val);
  };

  render() {
    return (
      <button onClick={() => this.subMain2Callback(42)}>
        SubMain2 - click me?
      </button>
    );
  }
}

【讨论】:

    【解决方案3】:

    您不应该使用this.handSubMainX,只需使用handleSumbmainX={this.handleSubmainX} 并将其绑定到您的构造函数中 结果如下:

    主要:

    class Main extends Component {
      constructor(props) {
      super(props);
      this.handleSubMain1 = this.handleSubMain1.bind(this);
      }
    
     handleSubMain1() {
      console.log('received props from SubMain1 which is from SubMain2!');
     }
    
      render() {
         return <SubMain1 handleSubMain1={this.handleSubMain1}/> <-- 2 Change here
      }
    }
    
    

    SubMain1:

    class SubMain1 extends Component {
    
     constructor(props) {
     super(props);
     this.handleSubMain2 = this.handleSubMain2.bind(this);
     }
    
     handleSubMain2() {
      console.log('received props from SubMain2!');
     }
    
      render() {
         return <SubMain2 handleSubMain2={this.handleSubMain2}/> <-- 2 Change here
      }
    }
    

    SubMain2:

    Component SubMain2 extends Component {
       constructor(props) {
       super(props);
       }
    
       render() {
         return this.props.handleSubMain2(); // this function should return jsx
      }
    }
    

    【讨论】:

    • 抱歉,您做了哪些更改?
    • 我将编辑代码以显示我在哪里进行了更改@jyoo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多