1.

Show using code how constants can be used in Redux.

Answer»

First of all, we can store all the constants in a single file in our project NAMED constants.js or SOMETHING else as follows:

export const ADDING_TODO = 'ADDING_TODO';export const DELETING_TODO = 'DELETING_TODO';export const EDITING_TODO = 'EDITING_TODO';export const COMPLETING_TODO = 'COMPLETING_TODO';export const COMPLETING_ALL = 'COMPLETING_ALL';export const CLEARING_COMPLETED = 'CLEARING_COMPLETED';

After storing the constants in one place, we can use them in two ways in our project:

  • During actions creation (in actions.js file of our project):
 IMPORT { DELETING_TODO } from './constants';export function deletingTodo(TEXT) { return { type: DELETING_TODO, text };}
  • In Reducers (in reducer.js file of our project):
 import { EDITING_TODO } from './constants';export default (state = [], action) => { switch (action.type) { CASE EDITING_TODO: return [ ...state, { text: action.text, completed: false } ]; default: return state }};


Discussion

No Comment Found