【发布时间】:2016-02-13 11:39:21
【问题描述】:
我正在将 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 传递,那么它可以工作,但根据我看到的所有示例,这似乎并不正确..
【问题讨论】:
-
正如我在你的减速器中看到的,你没有返回
state; -
从根减速器返回的内容正是传递给
connect的内容。如果您使用中间件/存储增强器,它可能会影响价值。 -
@MichelleTilley 谢谢,但是我删除了中间件 (
redux-thunk) 但仍然得到相同的结果。但你的评论给了我进一步调查的线索。
标签: reactjs react-native redux