Skip to content

Commit

Permalink
2nd step: Add state to Square Coponent.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrcel97 committed Aug 21, 2018
1 parent 883206b commit f2515ab
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Game extends React.Component {


class Board extends React.Component {

renderSquare(i) {
return <Square value={i} />;
}
Expand Down Expand Up @@ -50,13 +49,20 @@ class Board extends React.Component {
}

class Square extends React.Component {
constructor(props) { // We will use component state to store the current value of the Square
super(props); // You ALWAYS need to call 'super()' when defining the constructor of a subclass
this.state = {
value: null,
};
}

render() {
return (
<button className="square" onClick={() => alert('click')}> {/* Same as: onClick={function() { alert('click'); }}
* + Avoid confusing behavior
* + Save typing
* */}
{this.props.value}
<button
className="square"
onClick={() => this.setState({value: 'X'})}
> {/* One click, update Square state */}
{this.state.value} {/* Now we use state to know Square behaviour */}
</button>
);
}
Expand Down

0 comments on commit f2515ab

Please sign in to comment.