There are two types of data that control a component: props
and state
. props
are set by the parent and they are fixed throughout the lifetime of a component. For data that is going to change, we have to use state
.
In general, you should initialize state
in the constructor, and then call setState
when you want to change it.
For example, let's say we want to make text that blinks all the time. The text itself gets set once when the blinking component gets created, so the text itself is a prop
. The "whether the text is currently on or off" changes over time, so that should be kept in state
.
In a real application, you probably won't be setting state with a timer. You might set state when you have new data arrive from the server, or from user input. You can also use a state container like Redux to control your data flow. In that case you would use Redux to modify your state rather than calling setState
directly.
State works the same way as it does in React, so for more details on handling state, you can look at the React.Component API.
At this point, you might be annoyed that most of our examples so far use boring default black text. To make things more beautiful, you will have to learn about Style.
You can edit the content above on GitHub and send us a pull request!