我尝试将参数从子组件传递给父组件。附:如果有人解决这个问题,下面的代码段不起作用,那真的很棒。
class Parent extends React.Component {
suggestionClick(id) {
console.log(this.props, id); // {props Object} , undefined
}
render(){
return (
<ChildComponent click={this.suggestionClick.bind(this)} />
);
}
}
const ChildComponent = ({ click }) => (
<SubChildComponent id="1" click={() => click()} />
);
const SubChildComponent = ({ click, id }) => (
<div className="subsubcomponent" click={() => click(id)} />
);
ReactDOM.render(
<Parent />,
document.getElementById('app')
);
<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>
<div id="app"></div>
答案 0 :(得分:6)
class Parent extends React.Component {
suggestionClick(id) {
alert(id);
console.log(this.props, id); // {props Object} , undefined
}
render(){
return (
<ChildComponent click={this.suggestionClick.bind(this)} />
);
}
}
const ChildComponent = ({ click }) => (
<SubChildComponent id="1" click={click} />
);
const SubChildComponent = ({ click, id }) => (
<button className="subsubcomponent" onClick={() => click(id)}>click me</button>
);
ReactDOM.render(
<Parent />,
document.getElementById('app')
);
<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>
<div id="app"></div>
答案 1 :(得分:3)
这是因为您没有将值从ChildComponent传递给父组件,此处为:
<SubChildComponent id="1" click={() => click()} />
使用此:
<SubChildComponent id="1" click={(id) => click(id)} />
或者简单地说:
<SubChildComponent id="1" click={click} />
另一个变化是使用SubChildComponent中的onClick替换click。
工作代码:
class Parent extends React.Component {
suggestionClick(id) {
console.log(this.props, id); // {props Object} , undefined
}
render(){
return (
<ChildComponent click={this.suggestionClick.bind(this)} />
);
}
}
const ChildComponent = ({ click }) => (
<SubChildComponent id="1" click={(id) => click(id)} />
);
const SubChildComponent = ({ click, id }) => (
<div className="subsubcomponent" onClick={() => click(id)}>Click</div>
);
ReactDOM.render(
<Parent />,
document.body
);
&#13;
<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>
&#13;
答案 2 :(得分:1)
在ChildComponent
中,只需将click
属性直接传递给SubChildComponent
:
const ChildComponent = ({ click }) => (
<SubChildComponent id="1" click={click} />
);
答案 3 :(得分:0)
您也可以在父组件中使用 es6箭头功能,因此无需明确地将此功能绑定到此功能。
但是当我们从child调用此函数以及要传递给它的id时,我们将其绑定以传递id值。以下是修改后的代码供参考。希望这会有所帮助。
class Parent extends React.Component {
// make this function as arrow function
suggestionClick = (id) => {
console.log(this.props, id); // {props Object} , id
}
render(){
return (
<ChildComponent click={this.suggestionClick} />
);
}
}
const ChildComponent = (props) => (
<SubChildComponent id="1" click={props.click} />
);
const SubChildComponent = (props) => (
<div className="subsubcomponent" onClick={props.click.bind(this, props.id)} />
);
ReactDOM.render(
<Parent />,
document.getElementById('app')
);