即使使用正确的数据调用reducer,Redux状态也不会更新



由于某种原因,即使reducer运行并且console.log显示向其传递了正确的数据,redux存储也没有更新。

相关文件:

  1. App.jsx
import {Provider} from 'react-redux';
import store from './store';
const Stack = createStackNavigator();
export default class App extends Component {
render() {
return (
<Provider store={store()}>
  1. Store.js
import {createStore, applyMiddleware} from 'redux';
import rootReducer from '../reducers';
import thunk from 'redux-thunk';
const store = (initialState = {}) =>{
return createStore(
rootReducer,
initialState,
applyMiddleware(thunk)
)
}
export default store;
  1. 寄存器.tsx
...
<Pressable 
style={styles.button}
onPress={() => this.props.submitRegistration(this.state)}
>
...
const mapDispatchToProps = (dispatch: any) => {
return {
submitRegistration: (data: any) => {
dispatch(UserActions.submitRegister(data))
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Register);
  1. 用户操作
import { signUp } from '../../services/backend';
import { setUser } from '../../actions';
export function submitRegister(data: any) {
return async (dispatch: any) => {
const response = await signUp(data);
const responseData = await response.json();
if(responseData.token) {
console.log('here', responseData);
dispatch(setUser(responseData.user));
}
}; 
}
  1. 动作创建者
export const setUser = (user: any) => ({
type: 'SET_USER',
user
}); 
  1. 用户还原器
import { SET_USER } from "../actions/actionTypes"
const initialState = {
user: {}
}
const User = (state = initialState, action: any) => {
switch(action.type) {
case SET_USER: 
console.log('here action', action.user);
return { user: action.user}
default:
return state
}
}
export default User;

我真的很感激任何可能的帮助。似乎我在某种程度上配置错误,因为即使在我设置初始状态时:

const initialState = {
user: {firstName: "John"}
}

它没有反映在redux商店中。

在您的操作创建者中:

export const setUser=(user:any(=>(返回{类型:"SET_USER",使用者});

相关内容

  • 没有找到相关文章

最新更新