我已经开始将一些代码拆分为多个presentational/container组件,并且我想在child/presentational组件中调用一个函数,然后将事件和某种道具传递回父级。
上级:
class Parent extends Component{
constructor(props) {
super(props);
this.state = {}
this.reroll = this.reroll.bind(this);
}
test(key, e){
console.log(key, e)
}
render() {
return <Child test={()=>this.test} />
}
}
Run Code Online (Sandbox Code Playgroud)
儿童:
var Child = () => {
return (
<select onChange={props.test('test-key')}>
<option value='1'> Option 1 </option>
//etc...
</select>
)
}
Run Code Online (Sandbox Code Playgroud)
通常,当我将代码全部放在一个地方时,我会像这样编写onChange函数。
<select onChange={props.test.bind(this, 'test-key')}>
Run Code Online (Sandbox Code Playgroud)
但是将其绑定在子代中将使其不再起作用。传递给此函数的其他道具都不会返回给父对象。有什么办法可以写成这样,以便我可以取回“测试键”?