reactjs - ReactJS:通过 3 个组件传递数据
问题描述
我正在尝试通过层次结构中的 3 个组件(Parent-Child-Child2)传递数据:
父级:
class Parent extends React.Component {
constructor(props) {
super(props);
this.handleOperation = this.handleOperation.bind(this);
}
handleOperation = (value) => {
// some actions
};
render() {
return (
<div className={styles}>
<Child valueChild={this.handleOperation()}/>
</div>
);
}
}
孩子1:
class Child extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
handleChange =(value) => {
this.props.valueChild(value);
};
render() {
return (
<div>
<Child2 childValue2 = {this.handleChange} />
</div>
);
}
}
孩子2:
class Child2 extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}
handleChange =(event) => {
this.props.childValue2(event.target.value);
};
render() {
return (
<div>
<input type="button" onClick={this.handleChange} value="defaultValue" />
</div>
);
}
}
代码编译成功,但是当我单击输入字段(将值传递给父级)时,我收到以下错误:
解决方案
推荐阅读
- css - 如何检查多个单选按钮组
- macos - SwiftUI:将首选项菜单添加到 AppView
- javascript - 每当打开下拉菜单时如何在顶部设置下拉滚动条
- javascript - 无法读取未定义的属性“小”
- python - 在该模型属性中使用序列化程序
- node.js - 使用 moogoose 更新嵌套数组中的值
- postgresql - 如何在postgresql的存储过程中获取#行语句遇到错误?
- winforms - 单击打印按钮时WinForm ReportView抛出AccessViolationException
- java - 黄瓜 6:不包括依赖项
- javascript - 如何使用 Javascript 或 Jquery 获取所选选项的值?