我正在将 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>
);
}
}
我的 reducer 看起来像:
const initialState = {
key : "value"
};
export default function key(state = initialState, action = {}) {
switch (action.type) {
// ...
default:
return state;
}
}
商店是否以数组形式返回?我应该如何将其传递到 connect
方法中?
如果我将其作为 key: state[0].key
传递,那么它就可以工作,但根据我见过的所有示例,这似乎并不正确。
最佳答案
我应该早点发布根组件......它包含了线索。
常量 reducer =combineReducers([DataReducer])
应该是
const reducer =combineReducers({DataReducer})
这里的教训是不要着急,更仔细地查看文档!
关于reactjs - 我的 Redux 存储是一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35379372/