如何在成功发送 Redux 表单后重置表单
How to reset form after successful sent Redux Form
我正在使用 Redux 表单。
如果数据发送成功,我想重置表单。
import React, { Component } from "react";
import {
BrowserRouter as Router,
Route,
Switch,
withRouter
} from 'react-router-dom';
import CommentForm from '../components/CommentForm';
import { connect } from "react-redux";
import {authHeader} from '../components/auth-header';
import $ from 'jquery';
import { reset, reduxForm } from 'redux-form';
const createBlogComment = (data) => {
return fetch(CelestialSettings.URL.api + '/comments', {
method: 'post',
headers: {
...authHeader(),
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(res => {
if (res.status === 401) {
$('.thecmstatus').removeClass('success').addClass('error').html('There is something wrong while posting a comment');
return false;
}
if (res.status === 400) {
$('.thecmstatus').removeClass('success').addClass('error').html('There is something wrong while posting a comment');
return false;
}
if (res.status === 201) {
$('.thecmstatus').removeClass('error').addClass('success').html('Your comment will be visible after approval');
setTimeout(function() {
$('.thecmstatus').html('');
}, 3000);
return true;
}
}).catch(err => err);
}
class CommentFormToAPI extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(content) {
let APIdata = {...this.props.addComment, content:content.commentcontent};
createBlogComment(APIdata);
if(createBlogComment(APIdata)){
//I use this but it is wrong.
dispatch(reset('CommentForm'));
}
}
render() {
return (
<div>
<CommentForm onSubmit={this.handleSubmit}></CommentForm>
</div>
);
}
}
function mapStateToProps(state){
return {
addComment: state.addComment,
};
};
export default connect(mapStateToProps)(withRouter(CommentFormToAPI));
我可以处理自 API 发送后的提交,但我不知道如何重置 Redux 表单。你能给我推荐一下吗
我这里看了一下,我用的是最后一种方式(D)。
https://redux-form.com/6.0.0-alpha.4/docs/faq/howtoclear.md/
更新我的屏幕日志:
非常感谢。
将 dispatch
传递给您的 createBlogComment 函数:
createBlogComment(APIdata, dispatch)
然后,如果 201
响应:
if (res.status === 201) {
$('.thecmstatus').removeClass('error').addClass('success').html('Your comment will be visible after approval');
setTimeout(function() {
$('.thecmstatus').html('');
}, 3000);
dispatch(reset('YourFormName'));
return true;
}
确保从 redux-form
.
导入 reset
函数
我正在使用 Redux 表单。
如果数据发送成功,我想重置表单。
import React, { Component } from "react";
import {
BrowserRouter as Router,
Route,
Switch,
withRouter
} from 'react-router-dom';
import CommentForm from '../components/CommentForm';
import { connect } from "react-redux";
import {authHeader} from '../components/auth-header';
import $ from 'jquery';
import { reset, reduxForm } from 'redux-form';
const createBlogComment = (data) => {
return fetch(CelestialSettings.URL.api + '/comments', {
method: 'post',
headers: {
...authHeader(),
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(res => {
if (res.status === 401) {
$('.thecmstatus').removeClass('success').addClass('error').html('There is something wrong while posting a comment');
return false;
}
if (res.status === 400) {
$('.thecmstatus').removeClass('success').addClass('error').html('There is something wrong while posting a comment');
return false;
}
if (res.status === 201) {
$('.thecmstatus').removeClass('error').addClass('success').html('Your comment will be visible after approval');
setTimeout(function() {
$('.thecmstatus').html('');
}, 3000);
return true;
}
}).catch(err => err);
}
class CommentFormToAPI extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(content) {
let APIdata = {...this.props.addComment, content:content.commentcontent};
createBlogComment(APIdata);
if(createBlogComment(APIdata)){
//I use this but it is wrong.
dispatch(reset('CommentForm'));
}
}
render() {
return (
<div>
<CommentForm onSubmit={this.handleSubmit}></CommentForm>
</div>
);
}
}
function mapStateToProps(state){
return {
addComment: state.addComment,
};
};
export default connect(mapStateToProps)(withRouter(CommentFormToAPI));
我可以处理自 API 发送后的提交,但我不知道如何重置 Redux 表单。你能给我推荐一下吗
我这里看了一下,我用的是最后一种方式(D)。 https://redux-form.com/6.0.0-alpha.4/docs/faq/howtoclear.md/
更新我的屏幕日志:
非常感谢。
将 dispatch
传递给您的 createBlogComment 函数:
createBlogComment(APIdata, dispatch)
然后,如果 201
响应:
if (res.status === 201) {
$('.thecmstatus').removeClass('error').addClass('success').html('Your comment will be visible after approval');
setTimeout(function() {
$('.thecmstatus').html('');
}, 3000);
dispatch(reset('YourFormName'));
return true;
}
确保从 redux-form
.
reset
函数