I'm trying to integrate redux with my react native + typescript app.
(我正在尝试将redux与我的react native + Typescript应用程序集成在一起。)
From an specific component I'm dispatching an action:
(从一个特定的组件,我正在调度一个动作:)
export const setCurrentOrder = (order: PostOrderRequest) => async (dispatch: Dispatch) => {
console.log('order in action: ', order);
dispatch({
type: MENU_POST_ORDER,
order: order
});
}
That console.log is not being executed so do I have a mistake calling my action?
(该console.log没有被执行,所以我在调用操作时是否出错?)
This is how I'm calling setCurrentOrder from my component:
(这是我从组件中调用setCurrentOrder的方式:)
handleNext() {
const {pizza} = this.state;
const {navigation} = this.props;
const currentOrder = {
pizza
}
console.log('current order: ', currentOrder);
setCurrentOrder(currentOrder);
navigation.navigate('Login');
}
handleNext() is being called on a button's onPress event.
(在按钮的onPress事件上调用handleNext()。)
Then my reducer:
(然后我的减速器:)
const initialState: MenuState = {
order: {pizza: []}
}
export default function (state = initialState, action: MenuAction) {
console.log('REDUCER: ', action);
switch (action.type) {
case MENU_POST_ORDER:
return {
...state,
order: action.order
};
default:
return state;
}
}
By the way, that console.log inside the reducer function IS NOT BEING EXECUTED!
(顺便说一下,reducer函数内部的console.log没有被执行!)
I dont know why, but seems like the initialState is being setted but the reducer function is never executed. (我不知道为什么,但是好像在设置initialState,但是reducer函数从不执行。)
I executed setCurrentOrder from a component.
(我从组件执行了setCurrentOrder。)
Then after doing that, the component route the user to other component. (然后,执行此操作后,该组件会将用户路由到其他组件。)
From that other component I'm using react-redux to get the state: (从其他组件,我正在使用react-redux来获取状态:)
export default withNavigation(connect(
state => ({
state: state
}),
null
)(LoginScreen));
But when I log this.props.state
its showing the initialState.
(但是当我登录this.props.state
它显示的是initialState。)
I tried setting this.props.state to the component react state and using componentDidUpdate to check if the props change and if its, updating my react state. (我尝试将this.props.state设置为组件反应状态,并使用componentDidUpdate来检查道具是否发生变化,以及是否更改了我的反应状态。)
But its always showing my initialState so: (但是它总是显示我的initialState,所以:)
Why initialState is being setted but the reducer function is not being executed?
(为什么要设置initialState但未执行reducer函数?)
Edit:
(编辑:)
This is how I'm dispatching setCurrentOrder from my component (the withNavigation wrapper is because I'm using react-native-navigation):
(这就是我从组件中分派setCurrentOrder的方式(withNavigation包装器是因为我正在使用react-native-navigation):)
export default withNavigation(connect(
null,
(dispatch: Dispatch) => ({
setCurrentOrder: (order: PostOrderRequest) => dispatch<any>(setCurrentOrder(order))
})
)(MenuScreen));
ask by Darío GL translate from so