Understanding when to use local states
Before we consider React, let's see how JavaScript functions work. JavaScript functions can either be pure or impure. A pure function depends only on its arguments and returns the same value as long as the arguments are the same. A state holds a value outside arguments, and functions that depend on the state become impure. React components are also functions and can be pure. If we use a state in a React component, it will be impure. However, if the state is local to the component, it doesn't affect other components, and we call this characteristic "contained."
In this section, we learn JavaScript functions, and how similar React components are to JavaScript functions. We then discuss how a local state is conceptually implemented.
Functions and arguments
In JavaScript, a function takes an argument and returns a value. For example, here's a simple function:
const addOne = (n) => n + 1;
This is a...