skip to Main Content

I’m new on react redux and i got an error like on the below
enter image description here

and here is my store.js:

import { applyMiddleware, combineReducers, createStore } from "redux"
import thunk from "redux-thunk"
import {composeWithDevTools} from "redux-devtools-extension"
import authReducer from "./reducers/auth"


const initialState = {

}


const reducers = combineReducers({
    auth: authReducer
})

const store = createStore(reducers,initialState, composeWithDevTools(applyMiddleware(thunk)))


export default store

and here is my auth reducer:

const authReducer = (state = {auth:null}, action) =>{

switch (action.type) {
    case "REGISTER":
        localStorage.setItem('auth',JSON.stringify(action.payload))
        return{
            ...state,
            auth:action.payload
        }
    case "LOGIN":
        localStorage.setItem('auth',JSON.stringify(action.payload))
        return{
            ...state,
            auth: action.payload
        }
    case "LOGUT":
        localStorage.clear()
        return{
            ...state,
            auth: null
        }
    default:
        break;
}

}

export default authReducer

I wanted to take an login form datas but i got error like this. If anyone can help me I will appreciate

3

Answers


  1. I think you need to return state in default so it doesn’t return undefined.

    default:
        return state;
    
    Login or Signup to reply.
  2. You should return the state itself at the end if no action matched.

    switch (action.type) {
        // ...
    }
    return state;
    
    Login or Signup to reply.
  3. I think you should try edit your default of switch like:

    switch (action.type) {
        case "REGISTER":
            localStorage.setItem('auth',JSON.stringify(action.payload))
            return{
                ...state,
                auth:action.payload
            }
        case "LOGIN":
            localStorage.setItem('auth',JSON.stringify(action.payload))
            return{
                ...state,
                auth: action.payload
            }
        case "LOGUT":
            localStorage.clear()
            return{
                ...state,
                auth: null
            }
        default:
            return state;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search