Creating your first stateful React component
Stateful components are the most appropriate place for your application to handle the interaction logic and manage the state. They make it easier for you to reason out how your application works. This reasoning plays a key role in building maintainable web applications.
React stores the component's state in this.state,
and it sets the initial value of this.state
to the value returned by the getInitialState()
function. However, it's up to us to tell React what the getInitialState()
function will return. Let's add this function to our React component:
{ getInitialState: function () { return { isHidden: false }; }, render: function () { if (this.state.isHidden) { return null; } return React.createElement('h1', ...
Get React.js Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.