concept local state in category react

appears as: Local state, local state
Redux in Action

This is an excerpt from Manning's book Redux in Action.

React components have the concept of local, or component, state. Within any given component, you can keep track of the value of an input field or whether a button has been toggled, for example. Local state makes easy work of managing a single component’s behavior. However, today’s single-page applications often require synchronizing a complex web of state. Nested levels of components may render a different user experience based on the pages a user has already visited, the status of an AJAX request, or whether a user is logged in.

Note that hot module replacement currently has a few limitations. Updating non-component files may require a full-page refresh, for example, and a console warning may tell you as much. The other limitation to be aware of is the inability to maintain local state in React components.

Remember, it’s perfectly reasonable to use local component state in combination with the Redux store. Hot module replacement has no trouble leaving the Redux store intact, but maintaining local state after an update is a tougher puzzle to solve. When App and its children components are updated and re-rendered to the DOM, React sees these as new and different components, and they lose any existing local state in the process.

Listing 7.5. Search with local component state – src/components/TasksPage.js
... renderTaskLists() { const { onStatusChange, tasks } = this.props; const filteredTasks = tasks.filter(task => { #1 return task.title.match(new RegExp(this.state.searchTerm, 'i')); #2 }); return TASK_STATUSES.map(status => { const statusTasks = filteredTasks.filter(task => task.status === status); return ( <TaskList key={status} status={status} tasks={statusTasks} onStatusChange={onStatusChange} /> ); }); } #1 Before displaying tasks, filter down to those that match the search term. #2 Keeps track of the searchTerm in the local state of the App component !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":73},{\"line\":4,\"ch\":75}],[{\"line\":22,\"ch\":0},{\"line\":22,\"ch\":2}],[{\"line\":5,\"ch\":73},{\"line\":5,\"ch\":75}],[{\"line\":23,\"ch\":0},{\"line\":23,\"ch\":2}]]"} !@%STYLE%@!