让我卡住了好几天的问题是,虽然我的 redux devtool 显示成功的状态更新,没有任何突变并且 View 组件重新渲染成功,但是当我调用 getState() 它总是返回初始状态并且不在乎更新状态!任何知道什么会导致这种情况的人请帮助我。
我使用 react-redux 和 redux-thunk
动作.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() )
};
}
减速器.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);