Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native Redux useDispatch not working

I try to create a Redux and I have a problem when I try to do dispatch is not working.

Action file, userActions.js:

export const setId = () => {
   console.log("Enter to set id func");
   return {
      type: 'SET_ID'
   }
}

Reducer file, userReducer.js:

const INITIAL_STATE = {
    id: "",
    email: "",
    name: "",
};

const userReducer = (state = INITIAL_STATE, action) => {
    console.log('Enter to userReducer');
    switch (action.type) {
        case "SET_ID": {
            // console.log(action.payload);
        }
        default: {
            return state;
        }
    }
}

export default userReducer;

combineReducers file:

import userReducer from "./userReducer";
import { combineReducers } from "redux";

const allReducers = combineReducers({
    userReducer: userReducer
})

export default allReducers;

App.js file:

import React from 'react';
import Routes from "./Routes";
import { createStore } from "redux";
import allReducer from "./app/reducers";
import { Provider } from "react-redux";

const store = createStore(
   allReducer
);

const App = () => {
 return (
    <Provider store={store}>
      <Routes />
    </Provider>
 );
};

export default App;

In login screen file, I have button when I click on him call to dispatch to "setId" action.
Here some of my code from Login.js:

import { useDispatch } from 'react-redux';
import { setId } from '../actions/userActions';
handleLoginResult = (error, user) => {
        console.log('Enter to handleLoginResult');
        if (error !== "") {
            this.setState({ generalError: error });
        } else {
            const dispatch = useDispatch();
            console.log('uid: ', user.user.uid);
            dispatch(setId());
            alert("Login!");
        }
    }

What is the problem and why is not enter to setId action?

like image 308
david890ch Avatar asked May 09 '26 10:05

david890ch


1 Answers

You can try with like this

const userReducer = (state = INITIAL_STATE, action) =>dispatch =>{
console.log('Enter to userReducer');
switch (action.type) {
    case "SET_ID": {
        // console.log(action.payload);
    }
    default: {
        return state;
    }
}

}

like image 156
Sagar Roy Avatar answered May 12 '26 05:05

Sagar Roy