5 Making React interactive with states
This chapter covers
- The role of state in a component
- Using state in functional components
- Converting stateful class-based components to functional components
All the components we have created so far, take some properties and render some HTML based on those properties. We can pass a label property to a button, so the button is displayed with that exact button text, for instance. But we cannot make the button text change, when something happens, such as changing between "Turn on" and "Turn off" when toggled. That’s because we lack both the ability to react to something that happens, as well as the ability to store the single piece of information that something has happened.
The output of the components we have created so far depends on nothing but their properties. In other words, the components are "pure" in functional programming terms. The components have no other inputs and no side effects. If you give the same component the same properties, you will always get the same result and nothing but that result.