在react js redux中单击提交后如何清除输入字段
How to clear input field after clicking submit in react js redux
我正在开发一个学习react redux的小项目,我对连接react和redux有很好的看法。我正在尝试执行一个小任务,即在点击提交后清除输入框,我尝试使用重置,编写了单独的函数来清除状态,并在 handleSubmit 处理程序本身中包含了清除输入的逻辑,我没有得到预期的输出。谁能指导我如何实现这一目标。提前致谢。
AddRemainder.js File :
import React, { Component } from "react";
import { connect } from "react-redux";
import addRemainder from "../../redux/action.js";
class AddRemainderPage extends Component {
state = {
reminder: {
title: "",
},
};
handleChange = (e) => {
const reminder = { ...this.state.reminder, title: e.target.value };
this.setState({ reminder });
};
handleSubmit = (e) => {
e.preventDefault();
this.props.dispatch(addRemainder(this.state.reminder));
};
render() {
return (
<div className="App">
<div>
<h2>My First Redux UnderStanding</h2>
<form onSubmit={this.handleSubmit}>
<input
type="text"
onChange={this.handleChange}
value={this.state.reminder.title}
/>
<input type="submit" value="Save" />
</form>
</div>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
reminderss: state.reminderss,
};
};
export default connect(mapStateToProps)(AddRemainderPage);
Action.JS File:
const addRemainder = (text) => ({
type: "ADD_REMINDER",
rem: text,
});
export default addRemainder;
Reducer.JS File:
const intialState = {
reminderss: [],
};
const remindersReducer = (state = intialState, action) => {
switch (action.type) {
case "ADD_REMINDER":
return {
reminderss: [...state.reminderss, action.rem],
};
default:
return state;
}
};
export default remindersReducer;
试试这个方法:
clearInput = () => {
const reminder = { ...this.state.reminder, title: '' };
this.setState({ reminder });
};
handleSubmit = (e) => {
e.preventDefault();
this.props.dispatch(addRemainder(this.state.reminder));
this.clearInput();
};
我正在开发一个学习react redux的小项目,我对连接react和redux有很好的看法。我正在尝试执行一个小任务,即在点击提交后清除输入框,我尝试使用重置,编写了单独的函数来清除状态,并在 handleSubmit 处理程序本身中包含了清除输入的逻辑,我没有得到预期的输出。谁能指导我如何实现这一目标。提前致谢。
AddRemainder.js File :
import React, { Component } from "react";
import { connect } from "react-redux";
import addRemainder from "../../redux/action.js";
class AddRemainderPage extends Component {
state = {
reminder: {
title: "",
},
};
handleChange = (e) => {
const reminder = { ...this.state.reminder, title: e.target.value };
this.setState({ reminder });
};
handleSubmit = (e) => {
e.preventDefault();
this.props.dispatch(addRemainder(this.state.reminder));
};
render() {
return (
<div className="App">
<div>
<h2>My First Redux UnderStanding</h2>
<form onSubmit={this.handleSubmit}>
<input
type="text"
onChange={this.handleChange}
value={this.state.reminder.title}
/>
<input type="submit" value="Save" />
</form>
</div>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
reminderss: state.reminderss,
};
};
export default connect(mapStateToProps)(AddRemainderPage);
Action.JS File:
const addRemainder = (text) => ({
type: "ADD_REMINDER",
rem: text,
});
export default addRemainder;
Reducer.JS File:
const intialState = {
reminderss: [],
};
const remindersReducer = (state = intialState, action) => {
switch (action.type) {
case "ADD_REMINDER":
return {
reminderss: [...state.reminderss, action.rem],
};
default:
return state;
}
};
export default remindersReducer;
试试这个方法:
clearInput = () => {
const reminder = { ...this.state.reminder, title: '' };
this.setState({ reminder });
};
handleSubmit = (e) => {
e.preventDefault();
this.props.dispatch(addRemainder(this.state.reminder));
this.clearInput();
};