When the user clicks a button, I need the user to see that the button has been clicked.
The approach I am taking is to update the state for that component so that the button is re-rendered with a different background color. How is this actually done?
The CodePen for this is here
Here is the code:
var ActionBox = React.createClass({
showMyNumbers: function(){
if(this.props.pears === "default")
{console.log("This is in the default configuration")}
else{console.log("This is in some other configuration")}
},
render: function() {
return(
<div id="actionBox" onClick={this.showMyNumbers}>
</div>
);
},
});
var ApplicationGrid = React.createClass({
render: function() {
var row = [];
for(var j=0; j<30; j++){
for(var i=0; i<30; i++){
row.push(<ActionBox myRowNumber={j} myColumnNumber={i} pears = {this.props.oranges}/>);
}
}
return(
<div id="applicationGrid">
{row}
</div>
);
},
});
var ButtonsAndGrid = React.createClass({
getInitialState: function() {
var defaultcolor1 = document.getElementById(btn1).style.backgroundColor;
return {
btn1Bckground: defaultcolor1,
};
},
changecolor() {
this.setState({
btn1Bckground: "lightblue"
});
},
componentDidMount() {
this.refs.button1.addEventListener('click', this.changecolor);
},
render: function() {
return(
<div>
<div id="buttonsDiv">
<button type="button" ref="button1" className="normalBtn" id="btn1" onClick={this.props.plethora} >This works</button>
</div>
<ApplicationGrid oranges = {this.props.apples} />
</div>
);
},
});
var MyApp = React.createClass({
getInitialState: function() {
return {
startingConfiguration: "default",
};
},
doSomething: function(evt) {
this.setState({
startingConfiguration: "other"
});
},
render: function() {
return(
<div id="mainDiv" >
<h1> Game of Life! </h1>
<ButtonsAndGrid apples={this.state.startingConfiguration} plethora={this.doSomething}/> <Footer />
</div>
);
},
});
var Footer = React.createClass({
render() {
return (
<footer>
<div id="containerfooter">
<p>Written by <a href="http://codepen.io/profaneVoodoo/full/dXBJzN/">John Gillespie</a> for FreeCodeCamp Campers (and also to impress my kids). Happy Coding!</p>
</div>
</footer>
);
}
});
ReactDOM.render(
<MyApp /> ,
document.getElementById('GoL')
);
