I have a React app that has children of tabs and the display.
<App>
<Nav>
<Tab1></Tab1>
<Tab2></Tab2>
<Tab3></Tab3>
</Nav>
<Section>Display Stuff Here</Section>
<Footer></Footer>
</App>
When I made the tabs within the app, I have a for loop that appends the tabs in a Map variable (which is the child of the App React Component).
for(...) {
tab = React.createElement(Tab, {changeDisplay: () => {this.handleClick(this.changeDisplay(i)}});
myMap.set(i, tab);
}
What it should be doing is passing the i variable which increments from the for-loop. I even did a console.log to make sure it is pass 1, 2, 3, 4. However, what is actually displaying is the last variable: 4.
Within the Tab class, all I have access to use calling the props:
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});
}
}
The value for changeDisplay should be unique to each Tab, so Tab2 should only have a value of 2 and Tab3 should only have 3, etc. The for-loop should not be passing the next value to any previous Tabs already created since they are already made and appended to the map variable.
Any advice on how to make this work?
I could make custom functions:
changeDisplay1() { changeDisplay(1) };
changeDisplay2() { changeDisplay(2) };
changeDisplay3() { changeDisplay{3) };
changeDisplay4() { changeDisplay(4) };
However, I want to do it the right way instead of making hacks.