A
A
Andrew2017-08-18 22:22:28
JavaScript
Andrew, 2017-08-18 22:22:28

How correct is it to take store.getState() in action creators?

how correct is it to take store.getState() in action creators? is it possible to do so? this will make reducers cleaner, leave more logic in actions, what else can be pluses, and what can be minuses?
example

export function changePassWithCode(body = {}) {
    body.email = store.getState().authorizationReducer.emailForForgotPass;
    body.validationCode = store.getState().authorizationReducer.ForgotPassValidationCode;
    const
        url     = `${config.apiUrl}/user/myFuckingAPIOnPHP`,
        isRSAA  = true;

    return {type: POST_AUTHORIZATION_CHANGE_FORGOT_PASSWORD, url, isRSAA, body};
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eddy-lazar, 2017-08-19
@eddy-lazar

In general, this approach is not an anti-pattern, it depends on the situation. If the project uses redux-thunk, then it is more convenient to do it directly from the function, the second parameter of thunk is the getState method

export const SOME_ACTION = 'SOME_ACTION';
export function someAction() {
  return (dispatch, getState) => {
    const {items} = getState().otherReducer;
    dispatch(anotherAction(items));
  }
}

And you don't need to globally import store.
https://habrahabr.ru/post/330692/ - a good article on what is good in redux, what is bad and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question