Instagram
youtube
Facebook
Twitter

Store and Action

Redux- Store

  • The store is an unchangeable object tree in Redux.
  • It is a state container holding application's state.
  • Redux can only have a single store in the application and it needs to be specified with the reducer.
  • A reducer is to return to the next state of the app.
  • A store can be created using createStore method. First import the packages.
import { createStore } from 'redux';
import reducer from './reducers/reducer'
const store = createStore(reducer);
  • Syntax of createStore:
createStore(reducer, [preloadedState], [enhancer])

Methods of Store:

  • getState is to retrieve the current state from the redux store.
store.getState()
  • dispatch is used to dispatch an action to change a state.
store.dispatch({type:'ITEMS_REQUEST'})
  • subscribe helps to register a callback that the redux store will call when an action has been dispatched.
store.subscribe(()=>{ console.log(store.getState());})

Redux- Action

  • Action is a JavaScript object that has a type field that indicates the type of action performed.
const addTodoAction = {
  type: 'todos/todoAdded',
  payload: 'Buy milk'
}
  • Action creator is a function that creates and returns an action object.
  • It is used to write clean code and helps to achieve reusability.
const addTodo = text => {
  return {
    type: 'todos/todoAdded',
    payload: text
  }
}