gpt4 book ai didi

javascript - 商店更改后 React redux UI 未更新

转载 作者:行者123 更新时间:2023-12-01 04:07:54 25 4
gpt4 key购买 nike

我对 React 和 Redux 比较陌生,我创建了一个简单的 ajax 电子邮件表单来学习。我遇到的问题是,在提交表单后,我将商店状态设置回初始状态,这应该清除所有字段,但事实并非如此。我可以在 redux 记录器中看到存储更改,*请参阅附图,但这些更改未显示在用户界面上。我的商店是否未正确映射到州?或者我是否在某个地方改变了状态?

我的 reducer 如下所示:

export default function contactForm(state = initialState.formValues, action) {
switch (action.type) {
case types.FORM_RESET:
return initialState.formValues;
case types.FORM_SUBMIT_SUCCESS:
return Object.assign({}, action.message);
default:
return state;
}
}

组合 reducer :

import { combineReducers } from 'redux';
import message from './formReducer';
import ajaxCallsInProgress from './ajaxStatusReducer';

const rootReducer = combineReducers({
message,
ajaxCallsInProgress
});

我的初始状态如下:

export default {
formValues: {
name: '', email: '', message: '',
},
ajaxCallsInProgress: 0,
};

我的操作如下所示:

export function messageSuccess(message) {
return { type: types.FORM_SUBMIT_SUCCESS, message };
}

export function resetForm() {
return { type: types.FORM_RESET };
}

export function saveMessage(message) {
return function (dispatch) {
dispatch(beginAjaxCall());
return messageApi.saveMessage(message)
.then(() => {
dispatch(messageSuccess(message));
dispatch(resetForm());
}).catch((error) => {
dispatch(ajaxCallError(error));
throw (error);
});
}
}

在 View 中,我通过以下方式将状态映射到 Prop :

constructor(props, context) {
super(props, context);
this.state = {
message: Object.assign({}, this.props.message),
}
}

render() {
return (
<ContactForm
onChange={this.updateMessageState}
onSubmit={this.submitForm}
message={this.state.message}
/>
);
}

function mapStateToProps(state) {
return {
message: state.message,
};
}

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(formActions, dispatch)
};
}

export default connect(mapStateToProps, mapDispatchToProps)(ContactSection);

显示商店更改的日志

enter image description here

如果有任何建议,我将非常感激。

最佳答案

我已经用我认为应该适用于您的示例的代码更新了我的答案。您已经非常接近了,但是根据您对尝试组合两个 reducer 的评论,我创建了两个 reducer ,以便您可以看到它是如何工作的。

/* constants.js */
export default {
FORM_RESET: 'FORM_RESET',
FORM_SUBMIT: 'FORM_SUBMIT',
AJAX_REQUEST: 'AJAX_REQUEST'
};


/* form-values-reducer.js */
const initialState = {
name: '',
email: '',
message: ''
};

export default const formValuesReducer = (state = initialState, action) => {
switch (action.type) {
case Constants.FORM_SUBMIT:
return {
...state,
message: action.message
};

case Constants.FORM_RESET:
return {
..state,
name: '',
email: '',
message: ''
};

default:
return state;
}
};

/* ajax-request-reducer.js */
const initialState = {
ajaxRequestCount: 0
};

export default const ajaxRequestReducer = (state = initialState, action) => {
switch (action.type) {
case Constants.AJAX_REQUEST:
return {
...state,
ajaxRequestCount: state.ajaxRequestCount + 1
};
default:
return state;
}
};

/* action-creators.js */
export const resettedForm = () => {
return {
type: Constants.FORM_RESET
}
};

export const submittedForm = (message) => {
return {
type: Constants.FORM_SUBMIT,
message
}
};

export const ajaxRequested = () => {
return {
type: Constants.AJAX_REQUEST
}
};

/* actions */
export const resetForm = (dispatch) => {
return () => {
dispatch(resettedForm());
}
};

export const submitForm = (dispatch) => {
return (message) => {
dispatch(ajaxRequested());
dispatch(submittedForm(message));
}
};

/* reducers.js */
import { combineReducers } from 'redux';
import ajaxRequest from './ajax-request-reducer';
import formValues from './form-values-reducer';

export default combineReducers({
ajaxRequest,
formValues
});

/* Component */
import React from 'react';
import { connect } from 'react-redux';
import { resetForm, submitForm } from './actions';

const App = (props) => (
<div>Your app UI stuff goes here</div>
);

const mapStateToProps = (state) => {
return {
name: state.formValues.name,
email: state.formValues.email,
message: state.formValues.message,
ajaxRequestCount: state.ajaxRequest.ajaxRequestCount
};
};

const mapDispatchToProps = (dispatch) => {
return {
resetForm: resetForm(dispatch),
submitForm: submitForm(dispatch)
}
};

export default connect(mapStateToProps, mapDispatchToProps)(App);

我没有运行过任何东西,所以代码中可能存在一些错误。

关于javascript - 商店更改后 React redux UI 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com