What is component state?
React components declare the structure of a UI element using JSX. But this is only part of the story. Components need data if they are to be useful. For example, your component JSX might declare a <ul>
that maps a JavaScript collection to <li>
elements. Where does this collection come from?
State is the dynamic part of a React component. This means that you can declare the initial state of a component, which changes over time.
Imagine that we're rendering a component where a piece of state is initialized to an empty array. Later on, this array is populated with data. This is called a change in state, and whenever we tell a React component to change its state, the component will automatically re-render itself. The process is visualized here:
The state of a component is something that either the component itself can set, or other pieces of code, outside of the component. Now we'll look at component properties and how they differ from component...