【发布时间】:2021-07-02 23:09:40
【问题描述】:
我的 Redux 减速器有问题。在 selectedSongReducer 中 selectedSong 被定义为 null 因此它不会返回错误,但我仍然收到以下错误:错误:Reducer "selectedSong" 在初始化期间返回未定义。如果传递给 reducer 的状态未定义,则必须显式返回初始状态。初始状态可能不是未定义的。如果不想给这个reducer设置值,可以用null代替undefined。
这里是reducer代码:
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
})
任何人对可能出现问题的地方有任何建议吗?
【问题讨论】: