0

I have redux-thunk set up in my react-native code.

In mapDispatchToProps I have:

const mapDispatchToProps = dispatch => {
     return {
         login: (state, navigator) => dispatch({
             type: "USER",
             payload: loginFunc(state, navigator)
          }),
     }'
}

The login function is a firebase function that returns a promise chain that eventually returns the object I need.

However, in my reducer it looks like my payload is a promise or some funky object. It looks like:

{ 
     a: someVal, 
     b: sommeOtherVal, 
     c: someMoreVal,
     ...ValsUpToI.
     i: {
       // The values I actually need show up
     }

This messes me up because when I try to do payload.myValue I get undefined in my redux state. I don't think it is safe to just do payload.i either because this is probably some promise or async issue right? That i could very well be something else later?

1 Answer 1

1

Use asynchronous actions approach to execute promise based function API calls as mentioned in redux documentation.

const mapDispatchToProps = dispatch => {
 return {
     login: (state, navigator) => {
         return loginFunc(state, navigator).then(
             response => dispatch({
                 type: "USER",
                 payload: response
             }),
             error => console.log('An error occurred.', error)
        )
     }
 }

That way you can call promise based function and utilize response or error correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.