Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

Why my React-Redux action returns "undefined"?

I'm trying to get user_id from an async function. This is my action:


export const loginCC = (email, password) => {
  return async (dispatch) => {
    async function CC_Session() {
      return ConnectyCube.createSession({
        email: email,
        password: password,
      });
    }
    try {
      const response = await CC_Session();
      const uid = response.user_id; //THIS GIVES THE USER ID WITHOUT A PROBLEM
      const token = response.token; // THIS GIVES THE DATA TOO
      dispatch({
        type: LOGIN_TO_CONNECTYCUBE,
        ccid: uid,
        ccToken: token,
      });
    } catch (error) {
      console.log(error);
    }
  };
};

And this is my reducer:

const initialState = {
  token: null,
  userId: null,
  ccid: null,
  ccToken: null,
  isConnected: null,
};
export default (state = initialState, action) => {
  switch (action.type) {
    case LOGIN_TO_CONNECTYCUBE:
      return {
        ccid: action.ccid,
        ccToken: action.ccToken,
      };

I can get data from async function, and dispatch it. But when I want to access it with getState().auth.ccid, it returns "undefined". How can I solve this problem? Thanks!

question from:https://stackoverflow.com/questions/65863848/why-my-react-redux-action-returns-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved the problem. I got a Firebase login right after this action. Looks like the Firebase login action resets the state. I created another reducer called user. And dispatched the action above to that user reducer. I don't know why Firebase action resets the state, but it worked after dividing reducers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...