Chapter 3. Understanding Properties and State

React components rely on JSX syntax, which is used to describe the structure of the UI. JSX will only get us so far—we need data to fill in the structure of our React components. The focus of this chapter is component data, which comes in two varieties: properties and state.

We'll start things off by defining what is meant by properties and state. Then, we'll walk through some examples that show you the mechanics of setting component state, and passing component properties. Toward the end of this chapter, we'll build on your new-found knowledge of props and state and introduce functional components and the container pattern.

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:

What is component state?

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 state.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.208.117