I'm teaching myself React and I have come across something I am having trouble understanding. When I build my project with the code in getNavList() below I get the following error when clicking one of the buttons rendered in my component, "cannot read property of undefined 'click' react."
export class Pane extends React.Component {
constructor(props) {
super(props);
}
getNavList() {
var buttons = [];
if(this.props.subNavButtons != null){
for(var i = 0; i < this.props.navButtons.length; i++) {
buttons.push(<Button onClick={this.props.navButtons[i].click()} title={this.props.navButtons[i].title} />);
buttons.push(<Button onClick={this.props.navButtons[i + i].click()} title={this.props.navButtons[i + i].title} />);
buttons.push(<Button onClick={this.props.navButtons[i + i + 1].click()} title={this.props.navButtons[i + i + 1].title} />);
}
} else {
buttons = this.props.navButtons.map(button => (<Button onClick={() => button.click()} title={button.title}/>));
}
return buttons;
}
render() {
return (
<div>
<ul>
{this.getNavList()}
</ul>
</div>
);
}
}
When I build my project with the following changes, everything works as intended:
export class Pane extends React.Component {
constructor(props) {
super(props);
}
getNavList() {
var buttons = [];
if(this.props.subNavButtons != null){
buttons = this.props.navButtons.map((button, index) => (<>
<Button onClick={() => button.click()} title={button.title}/>
<Button onClick={() => this.props.subNavButtons[index + index].click()} title={this.props.subNavButtons[index + index].title}/>
<Button onClick={() => this.props.subNavButtons[index + index + 1].click()} title={this.props.subNavButtons[index + index + 1].title}/>
</>));
}
else {
buttons = this.props.navButtons.map(button => (<Button onClick={() => button.click()} title={button.title}/>));
}
return buttons;
}
render() {
return (
<div>
<ul>
{this.getNavList()}
</ul>
</div>
);
}
}
From what I've been able to find, it seems like there's an issue with 'this' losing it's context when I pass the function as a component property. I tried binding the function passed to the Pane object in its parent class to no avail. I also tried defining the function using arrow notation instead with no luck. With that being said, why is it that this change from a for loop to the map function work?
Here's a more minor issue within the same code snippet. With the working getNavList() I return a React.Fragment using the shorthand syntax "< >...< />" but when I try using "< React.Fragment>...< /React.Fragment>" I get an error saying that there is an exppected '}' character at the end of my else statement. What the heck is that about?
Please excuse and spacing errors in my code snippets and React tags. I couldn't figure out how to get them to display without adding additional spaces and I am almost entirely sure that this is not a syntax error but feel free to prove me wrong if that is the case.