我正在使用Recompose设置React Native中的用户登录屏幕,其中包含单独的动作和reducer文件,但我的reducer永远不会被调用。目前,还只是触发doUserLogin()
重新合成处理登录按钮:React Native重构动作不触发reducer
loginScreen.js
import React from 'react';
import { Button, Text, View } from 'react-native';
import { connect } from 'react-redux';
import { withHandlers, compose } from 'recompose';
import { loginUser } from './user/userActions';
const LoginScreen = ({ user, doUserLogin }) => {
return (
<View style={styles.loginContainer}>
{user ? <Text>Hi, {user.name}!</Text> : <Text>NOT Logged in!</Text>}
<Button title="Log In" onPress={doUserLogin} />
</View>
);
};
export default compose(
connect((state, props) => ({
...state.user,
})),
withHandlers({
doUserLogin: props =>
(event) => {
console.log('LOGIN USER IN HANDLER'); // <-- THIS IS WORKING
loginUser();
},
}),
)(LoginScreen);
的doUserLogin()
处理程序依次调用我的行动loginUser()
文件:
userActions.js:
import { LOGIN_REQUEST } from './actionTypes';
export const loginUser =() => {
return (dispatch) => {
console.log('In action'); // <-- THIS IS WORKING
dispatch({ type: LOGIN_REQUEST });
};
};
到目前为止,这么好。但是,当我dispatch()
,我的减速机从来没有被称为。但减速为捡其他行动(从导航等) - 它根本不是从loginUser()
收到上述动作:
userReducer.js:
import { LOGIN_REQUEST } from './actionTypes';
const userReducer = (state = initialState, action) => {
console.log('In reducer'); <-- ** THIS IS NEVER CALLED **
switch (action.type) {
case LOGIN_REQUEST:
return Object.assign({}, state, {
isFetching: true,
});
case LOGOUT:
return initialState;
default:
return state;
}
};
export default userReducer;
任何建议将不胜赞赏。
嗨,何塞。感谢你的回答!不确定你到底在说什么。你可以用一些代码来扩展答案吗? – GollyJer
@GollyJer在代码中添加了一个小例子... –