Skip to content Skip to sidebar Skip to footer

How Can I Avoid Slow Get State From The Redux Store?

I have App contains Tabs that gets data from API bassed on token I passed to the header request, So in the login screen, i dispatch an action to save token after user login and it'

Solution 1:

When response token get then redirect to page. Maybe should add callback function for action. For example:

This following code is for add record

addVideoGetAction(values, this.props.data, this.callBackFunction)

This callBackFunction is close the modal

callBackFunction = (value: any) => {
    this.setCloseModal();
};

You will use callback function in login action. This function will redirect to the page

This function call in saga. this following code

function* setAddOrUpdate(params: any) {
        params.callback(redirectPageParams);
}

Solution 2:

In redux we should NEVER alter the state object in reducer ... we return a brand new object

constsaveTokenReducer = (state = initial_state, action) => {
  const {payload, type} = action;
  switch (type) {
    caseSAVE_TOKEN:
      state = {
        ...state,
        token: payload,
      };
      break;
  }
  return state;
};

Instead

const saveTokenReducer = (state = initial_state, action) => {
  const { payload, type } = action;
  switch (type) {
    case SAVE_TOKEN:
      return { ...state, token: payload };
    default:
      return state;
  }
};

Regarding dispatching two actions at the same time

constuserReducer = (state = initial_state, { action, type }) => {
  switch (type) {
    caseUPDATE_LOGIN:
      return { ...state, token: payload, isLogin: !!payload };
    default:
      return state;
  }
};

-

facebookAuth = async () => {
  this.props.dispatch(updateLogin(token));
};

-

import { UPDATE_LOGIN } from'./types';

exportconstupdateLogin = token => {
  return {
    type: UPDATE_LOGIN,
    payload: token,
  };
};

Post a Comment for "How Can I Avoid Slow Get State From The Redux Store?"