Redux reducer returns undefined error even it is initialized as null

I have a problem with my Redux reducer. In the selectedSongReducer the selectedSong is defined as null so it will not return an error, but I still get the following error: Error: Reducer "selectedSong" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.

Here is the reducer code:

import { combineReducers } from 'redux';

const songsReducer = () => {
    return [
        { title: 'A' , length: '4:05' },
        { title: 'B' , length: '2:30' },
        { title: 'C' , length: '3:15' },
        { title: 'D' , length: '1:45' },
    ]
};

const selectedSongReducer = (selectedSong = null, action) => {
    if(action.type = 'SONG_SELECTED') {
        return action.payload;
    }
    return selectedSong;
}

export default combineReducers({
    songs: songsReducer,
    selectedSong: selectedSongReducer
})

Anybody any suggestions on where could be the problem?

Upvotes: 0

Views: 466

Answers (1)

Nive
Nive

Reputation: 31

You have accidentally used an assignment operator instead of comparing the values here

if(action.type = 'SONG_SELECTED')

It will always be executed.

Upvotes: 2

Related Questions