I'm attempting to recreate a calculator I made in Angular using React. I found recreating the html was not too difficult, but I'm having an issue with binding onclick events inside a looped Element.
Here's the element I want to recreate:
class Button extends React.Component {
render(){
return <div onClick={this.props.func} className="calc-button">{this.props.value}</div>;
}
}
Here's where I want it to loop and bind a function to the button.
class OperationsBox extends React.Component {
constructor(props){
super(props);
this.state = objButton;
this.numClick = this.numClick.bind(this);}
numClick(){
console.log("5")
}
render(){
var rows = [];
this.state.button.forEach(function(button,index){
var row = button.map(function(obj, index2)
{
if(index===4){
return <div className="calc-button clear">{obj.value}</div>;
}
return(<Button
func={this.numClick.bind(this, index2)}
value={obj.value}
/>);
}.bind(this))
rows.push(row)})
return <div className="operations-box">
<div className="calc-row">{rows[0]}</div>
<div className="calc-row">{rows[1]}</div>
<div className="calc-row">{rows[2]}</div>
<div className="calc-row">{rows[3]}</div>
<div className="calc-row">{rows[4]}</div>
</div>;
}
}
It constantly says the numClick is undefined.
Here's the link to my React Calc
http://codepen.io/awronka/pen/XmyQxB?editors=001
Here's my link to my working Angular Calc