concept schema in category react

This is an excerpt from Manning's book Redux in Action.
The first step on the road to normalization is to define a normalizr schema. Schemas are how you tell normalizr what shape to give back when you run the API response through the normalize function. You’ll put your schemas along with the actions in src/actions/index.js for now, but it’s also common to use a separate file for schemas. The following listing tells normalizr that you have two top-level entities, tasks and projects, and that tasks belong to projects.
Listing 8.25. Adding normalizr schemas – src/actions/index.js
import { normalize, schema } from 'normalizr'; ... const taskSchema = new schema.Entity('tasks'); const projectSchema = new schema.Entity('projects', { tasks: [taskSchema], });Next you run the API response from the /projects endpoint through normalizr’s normalize function, which takes an object and a schema and returns a normalized object. This is the piece that transforms the nested API response into the normalized structure you’ll use in Redux. Also, create a new receiveEntities action creator, which will return a RECEIVE_ENTITIES action. You’ll then handle this action in both reducers, tasks and projects, as shown in the following listing. This helps you reduce boilerplate by not having to dispatch multiple actions, such as FETCH_PROJECTS_SUCCEEDED and FETCH_TASKS_SUCCEEDED.
Listing 8.26. Normalizing the response – src/actions/index.js
function receiveEntities(entities) { #1 return { type: 'RECEIVE_ENTITIES', payload: entities, }; } ... export function fetchProjects() { return (dispatch, getState) => { dispatch(fetchProjectsStarted()); return api .fetchProjects() .then(resp => { const projects = resp.data; const normalizedData = normalize(projects, [projectSchema]); #2 dispatch(receiveEntities(normalizedData)); #3 if (!getState().page.currentProjectId) { #4 const defaultProjectId = projects[0].id; #4 dispatch(setCurrentProjectId(defaultProjectId)); #4 } #4 }) .catch(err => { fetchProjectsFailed(err); }); }; } #1 Creates a generic receiveEntities action #2 Passes the response and the schema into normalizr #3 Dispatches the normalized result #4 Sets a default project ID !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":73},{\"line\":0,\"ch\":75}],[{\"line\":33,\"ch\":0},{\"line\":33,\"ch\":2}],[{\"line\":18,\"ch\":73},{\"line\":18,\"ch\":75}],[{\"line\":34,\"ch\":0},{\"line\":34,\"ch\":2}],[{\"line\":20,\"ch\":73},{\"line\":20,\"ch\":75}],[{\"line\":35,\"ch\":0},{\"line\":35,\"ch\":2}],[{\"line\":22,\"ch\":73},{\"line\":22,\"ch\":75}],[{\"line\":23,\"ch\":73},{\"line\":23,\"ch\":75}],[{\"line\":24,\"ch\":73},{\"line\":24,\"ch\":75}],[{\"line\":25,\"ch\":73},{\"line\":25,\"ch\":75}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":22,\"ch\":73},{\"line\":22,\"ch\":75}],[{\"line\":23,\"ch\":73},{\"line\":23,\"ch\":75}],[{\"line\":24,\"ch\":73},{\"line\":24,\"ch\":75}],[{\"line\":25,\"ch\":73},{\"line\":25,\"ch\":75}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":22,\"ch\":73},{\"line\":22,\"ch\":75}],[{\"line\":23,\"ch\":73},{\"line\":23,\"ch\":75}],[{\"line\":24,\"ch\":73},{\"line\":24,\"ch\":75}],[{\"line\":25,\"ch\":73},{\"line\":25,\"ch\":75}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":22,\"ch\":73},{\"line\":22,\"ch\":75}],[{\"line\":23,\"ch\":73},{\"line\":23,\"ch\":75}],[{\"line\":24,\"ch\":73},{\"line\":24,\"ch\":75}],[{\"line\":25,\"ch\":73},{\"line\":25,\"ch\":75}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}]]"} !@%STYLE%@!