- Next, we need to change what happens when a Square is clicked.
- Immutable data can easily determine if changes have been made, which helps to determine when a component requires re-rendering.
- These are semantically equivalent; the second is called object literal syntax and is more convenient.
If we were displaying data from a database, Alexa, Ben, and Claudia’s database IDs could be used as keys. Placing the history state into the Game component lets us remove the squares state from its child Board component.
We may think that Board should just ask each Square for the Square’s state. Although this approach is possible in React, we discourage it because the code becomes difficult to understand, susceptible to bugs, and hard to refactor. Instead, the best approach is to store the game’s state in the parent Board component instead of in each Square. The Board component can tell each Square what to display by passing a prop, just like we did when we passed a number to each Square. We use components to tell React what we want to see on the screen. When our data changes, React will efficiently update and re-render our components. Don’t delete the entire src folder, just the original source files inside it.
Apply() has a sister function named call, which again lets you set this but takes an expanded argument list as opposed to an array. Used inside a function, this refers to the current object. What that actually means is specified by the way in which you called that function. If you called it using dot notation or bracket notation on an object, that object becomes this. If dot notation wasn’t used for the call, this refers to the global object. In the above code, the variable args holds all the values that were passed into the function.
- Was clear and articulate and made sure he had helped with all her problems before ending the lesson.
Adding Time Travel
As we iterate through history array, step variable refers to the current history element value, and move refers to the current history element index. We are only interested in move here, hence step is not getting assigned to anything.
This allows more optimizations to be done by the engine and results in more readable code. The name also shows up in the debugger and some stack traces, which can save you time when debugging. The named parameters turn out to be more like guidelines than anything else. You can call a function without passing the parameters it expects, in which case they will be set to undefined.