我的Redux商店是一个阵列

时间:2016-02-13 11:39:22

标签: reactjs react-native redux

我正在将Redux集成到我的React Native应用程序中。

我一直无法将状态传递给我的组件,并意识到它是因为初始状态以' 0'键开头,例如

{
  '0': {
    key: 'value'
  }
}

即。它似乎是一个阵列。

如果我的connect导出:

export default connect(state => ({
    key: state.key,
    reduxState: state
  }),
  (dispatch) => ({
    actions: bindActionCreators(MyActions, dispatch)
  })
)(MyReduxApp);

key始终未定义,但reduxState成功传递下来。 (当然不建议将整个州送下来)

根组件:

import React, { Component } from 'react-native';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';

import DataReducer from '../Reducers/Data';
import MyRedApp from './JustTunerApp';

const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const reducer = combineReducers([DataReducer]); // ready for more reducers.. 
// const store = createStoreWithMiddleware(reducer);
const store = createStore(reducer);

export default class App extends Component {
  render() {
    console.log ("store.getState:")
    console.log (store.getState())
    return (
      <Provider store={store}>
        <MyReduxApp />
      </Provider>
    );
  }
}

我的减速机看起来像:

const initialState = {
  key : "value"
};

export default function key(state = initialState, action = {}) {
  switch (action.type) {
    // ...
    default:
      return state;
  }
}

商店是否以数组形式返回?我应该如何将其传递给connect方法?

如果我将其作为key: state[0].key传递,那么它可以正常工作,但根据我见过的所有示例,这看起来并不合适。

1 个答案:

答案 0 :(得分:1)

我应该早点发布根组件......这样做了。

const reducer = combineReducers([DataReducer])

应该是

const reducer = combineReducers({DataReducer})

这里的教训是不要急于查看文档!