0
votes

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;
2
have you setup redux-thunk? - Tomasz Mularczyk
no, I don't have redux-thunk in my app - kurniawan26
show how you create your redux store. - Tomasz Mularczyk

2 Answers

11
votes

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)
);
0
votes

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. `