我有一个React应用程序,其中包含选项卡和显示的子项。如何将参数传递给React子的绑定函数?
<App>
<Nav>
<Tab1></Tab1>
<Tab2></Tab2>
<Tab3></Tab3>
</Nav>
<Section>Display Stuff Here</Section>
<Footer></Footer>
</App>
当我创造应用中的标签,我有一个for循环,追加在地图变量的标签(这是应用程序的子阵营组件)。
for(...) {
tab = React.createElement(Tab, {changeDisplay:() => {this.handleClick(this.changeDisplay(i)}});
myMap.set(i, tab);
}
它应该做的是传递从for循环递增的i变量。我甚至做了一个console.log以确保它通过了1,2,3,4。然而,实际显示的是最后一个变量:4.
在Tab类中,我有权访问调用道具:
class Tab extends React.Component {
constructor(props) {
super(props);
this.tab = React.Component('div', null);
}
render() {
return this.tab;
}
componentDidMount() {
this.forceUpdate();
}
componentWillUpdate(nextProps, nextState) {
this.tab = React.Component('div', {onClick: nextProps.changeDisplay});
}
}
为changeDisplay的值应该是唯一的每个标签,以便TAB2应该只具有2的值,并且TAB3应该只有3等的for循环不应该是使下值添加到已经创建的任何以前的标签,因为它们已经制作并附加到地图变量中。
有关如何使这项工作的任何建议?
我可以自定义函数:
changeDisplay1() { changeDisplay(1) };
changeDisplay2() { changeDisplay(2) };
changeDisplay3() { changeDisplay{3) };
changeDisplay4() { changeDisplay(4) };
不过,我想这样做的正确的方式,而不是使得黑客。
相关:https://stackoverflow.com/q/750486/5647260 – Li357
任何你不以另一种方式去做的理由?还是你坚持让它以这种方式工作? – Win