Skip to content

Commit

Permalink
2nd step: Handle Square clickEvents by Board Component.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrcel97 committed Aug 22, 2018
1 parent 10b0dc9 commit f1c64d3
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class Board extends React.Component {
}

renderSquare(i) {
return <Square value={this.state.squares[i]} />; // Board know Squares initial state so we can define it.
return (
<Square
value={this.state.squares[i]} // Set the 'this.props.value' of the Square Component to 'X', 'O' or Null.
onClick={() => this.handleClick(i)} // Function defined by Board Component to help us to update Board state without privacity problems.
/>
);
}

render() {
Expand Down Expand Up @@ -67,9 +72,23 @@ class Square extends React.Component {
return (
<button
className="square"
onClick={() => this.setState({value: 'X'})}
onClick={() => this.props.onClick()} // onClick() will generate a 'click event' handled by handleClick() function in Board Component.
>
{this.state.value}
{this.props.value} {/* Now we are interessed in work with 'this.props.value' due to be updated by the BoardComponent
*
* Explication:
* · State:
* - Individual local states.
* - Often become the props of the Child Components.
* - Inmutable (by any other Component)
* - Mutable (by the same Component)
*
* · Props:
* - External given stats.
* - Always given by a Parent Component.
* - Inmutable (by the same or other components)
* - Mutable (by the Parent Component) <- We want this data flow
* */}
</button>
);
}
Expand Down

1 comment on commit f1c64d3

@Mrcel97
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point the web application will fail if we make onClick event on any cell. This is because handleClick function will be defined in the next step.

Please sign in to comment.