【发布时间】:2018-09-28 06:25:33
【问题描述】:
让我卡住了好几天的问题是,虽然我的 redux devtool 显示成功的状态更新,没有任何类型的突变并且视图组件重新渲染成功,但是当我调用 getState() 时它总是返回初始状态并且没有不关心更新的状态!任何知道什么会导致这种情况的人请帮助我。
我使用 react-redux 和 redux-thunk
action.js
export function test(data) {
return {
type: 'TEST',
data
};
}
export function testFunc(data) {
return dispatch => {
dispatch(test(data))
console.log('GLOBAL STATE IS :', store.getState() )
};
}
reducer.js
export default function peopleReducer(state = initialState, action) {
switch (action.type) {
case 'TEST':
return {
...state,
test: action.data
}
default:
return state;
}
}
Page01.js
componentDidUpdate(){
console.log('get state = ', store.getState())
}
....
<TouchableHighlight
underlayColor={"#0e911b"}
onPress={() => {
this.props.testing('test contenttttt !!!!')
}}
>
<Text style={styles.title}>ACTION</Text>
</TouchableHighlight>
function mapStateToProps(state) {
return {
people: state.people,
planets: state.planets,
test: state.test
};
}
function mapDispatchToProps(dispatch) {
return {
getPeople: () => dispatch(getPeopleFromAPI()),
getPlanets: () => dispatch(getPlanetsFromAPI()),
testing: data => dispatch(testFunc(data)) };
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
【问题讨论】:
-
你在哪里使用getState,你能在你看到这个问题的地方显示一个示例代码sn-p
-
在上面的sn-p中没有看到你使用getState
-
在 Page01.js
-
具体在哪里,不在您提到的代码中
-
对不起,我的意思是在 dispatch 之后就在 action.js 中
标签: javascript reactjs react-native redux getstate