0

Im absolutely a beginner in React Native, Redux, and friends.
Now I try to create an app with React Native and Redux.
In the process I found this error :

enter image description here

I try to use Redux in my app like this :

export const confirmRegister = (userdata) => {
    return (dispatch) => {
        return(
            fetch(URI + path, {
                method: 'POST',
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
                    'x-application-id' : AppId,
                    'hashing' : this.generateHashing(userdata)
                },
                body: userdata,
            })
            .then((response) => {
                return response.json()
            })
            .then((response)=>{
                dispatch(setDataAfterRegister(response.result))
            })
            .catch(error => {alert(error)})
        )
      };
    };

    export function setDataAfterRegister(data){
      return{
        type:'REGISTERED',
        name:data.name,
        token:data.access_token
      }
    }

I'm still learning how to use Redux.
Any helps will be really appreciated.


Here's my store:

import { createStore } from 'redux';
import rootReducer from './reducers';
 
let store = createStore(rootReducer);
 
export default store;

3
  • have you setup redux-thunk? Commented Jun 3, 2018 at 8:41
  • no, I don't have redux-thunk in my app Commented Jun 3, 2018 at 8:42
  • show how you create your redux store. Commented Jun 3, 2018 at 8:43

2 Answers 2

11

confirmRegister function returns a function:

export const confirmRegister = (userdata) => {
  return (dispatch) => {

Redux don't know how to handle it natively. You need a middleware to do that. Most popular middleware that will let you return a function from your action creators is a redux-thunk.

First install it:

npm install --save redux-thunk

and then set up middleware:

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index';

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much, you saved me :D... one more, how do I catch the updated state ?
@kurniawan26 No problem. If you want to access current state use react-redux and connect
it works, now I want to navigate to home screen after the states updated, I got this warning when Im on the home screen : Warning:Cannot update during an existing state transition any idea ?
@kurniawan26 you probably try to update unmounted component.
sorry, I dont get your point sir.. is this something I really need to fix ? or I can just ignore the warning ?
|
0

Your Action creator confirmRegister returns a function, But in React action creator returns a action that is a plain JS Object.

So you need some middleware to tell react about this asynchronous call. You can use Redux-thunk , redux-Sagas and other middlewares. This will let your action creator return a function and that function on getting response will dispatch a new action object.

You can install the middlerware using npm or yarn

npm install --save redux-thunk
yarn add redux-thunk

Add that to your store config file.

And for that Warning:Cannot update during an existing state transition you have to setState of a component either in ComponentWillReceiveProps or ComponentWillMount. You are probably setting the state in render method. `

3 Comments

where should I call ComponentWillReceiveProps ? is it on my current screen or on the screen that I want to navigate to ?
The Current Screen from which you want navigation to happen
woaaaaaahhhh... thank you so much.. the warning dissapear.. I wish you have a good day sir

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.