React + Redux值未定义mapStateToProps

时间:2019-03-09 10:59:56

标签: reactjs redux

所以我有这个

index.js

import {getShowsHome} from './reducers/reducers';

const store = createStore(getShowsHome);
ReactDOM.render(<Provider store={store}> <App /> </Provider>, document.getElementById('root'));

App.js

const mapStateToProps = (state) => {
 return {
  tvSeries: state.getShowsHome.tvSeries,
  isPedding: state.getShowsHome.isPedding,
  movies: state.getShowsHome.movies,
  horror: state.getShowsHome.horror,
  scifiFantasySeries: state.getShowsHome.scifiFantasySeries
 }
}
const mapDispatchToProps = (dispatch) => {
 return {
 getShows: () => dispatch(getShows())
 }
}

减速器

const initialState = {
isPedding: true,
tvSeries: [],
movies: [],
horror: [],
scifiFantasySeries: []
}
export const getShowsHome = (state=initialState, action={}) => {
switch(action.type) {
    case GET_SHOWS_HOME_PEDDING:
        return Object.assign({}, state, {isPedding: true});
    case GET_SHOWS_HOME_SUCCESS:
        return Object.assign({}, state, {
                                tvSeries: action.payload.popularSeries,
                                movies: action.payload.popularMovies,
                                horror: action.payload.horrorMovies,
                                scifiFantasySeries: action.payload.scifiFantasySeries,
                                isPedding: false,
                            });
    case GET_SHOWS_HOME_FAILED:
        return Object.assign({}, state, {error: action.payload});
    default: 
    return state;
}
}

所以问题是我得到了一个错误,我得到了tvSeries(仅因为它是第一个),它是未定义的,就像商店没有在App上通过减速器一样,为什么

1 个答案:

答案 0 :(得分:3)

您的商店将直接具有状态,而不是在“ getShowsHome”键下,而只是函数名称。所以尝试

 tvSeries: state.tvSeries

如果您有多个减速器,请使用CombineReducers(从redux导入)

const store = createStore(combineReducers({ getShowsHome: getShowsHome, ....other here }))