在React

时间:2018-09-25 18:02:23

标签: javascript reactjs forms

我想创建一个与我一起使用的函数,可以在不提交的情况下重置表单输入中的值。我尝试在App Component(resetFormFields)中创建该函数,然后将其通过props传递给Form Component。当我想在onSubmit(e.target.reset())上执行此操作时,这是很可爱的,但是当我必须在不提交的情况下执行此操作时,却陷入了与表单不同的元素上。我可以在不添加这些值的状态下做到这一点吗?

应用程序:

class App extends Component {

state = {
    people: [],
    formMessages: [],
    person: null
};

handleFormSubmit = e => {

    e.preventDefault();

    const form = e.target;
    const name = form.elements["name"].value;
    const username = form.elements["username"].value;

    this.addPerson(name, email);
    form.reset();
};

resetFormFields = () => {
    return;
}

render() {

    return (
        <div className="App">
            <Form formSubmit={this.handleFormSubmit} 
                  reset={this.resetFormFields} />
        </div>
    );
}

表格:

    const Form = props => (
  <form className={classes.Form}
    id="form"
    onSubmit={props.formSubmit}>

    <input autoFocus
        id="name"
        type="text"
        defaultValue=""
        placeholder="Name..."
    />
    <input
        id="email"
        type="text"
        defaultValue=""
        placeholder="Email..."
    />
    <Button
        btnType="Submit"
        form="form"
        type='submit'>
        Submit
    </Button>
    <label onClick={props.reset}>Reset fields</label>
</form> );

3 个答案:

答案 0 :(得分:1)

您应该根据组件状态设置输入值,然后只需更新组件状态

class App extends Component {

state = {
    people: [],
    formMessages: [],
    person: null,
    name: "",
    email: "",
};

updateState = (newState) => {
    this.setState(newState);
}

handleFormSubmit = e => {

    e.preventDefault();

    this.addPerson(this.state.name, this.state.email);
    form.reset();
};

resetFormFields = () => {
    this.setState({name:"", email: ""});
}

render() {

    return (
        <div className="App">
            <Form formSubmit={this.handleFormSubmit}  updateState={this.updateState}
                  reset={this.resetFormFields} email={this.state.email} name={this.state.name} />
        </div>
    );
}

然后

const Form = props => (
  <form className={classes.Form}
    id="form"
    onSubmit={props.formSubmit}>

    <input autoFocus
        id="name"
        type="text"
        defaultValue=""
        value={this.props.name}
        onChange={(e) => this.props.updateState({name: e.target.value})}
        placeholder="Name..."
    />
    <input
        id="email"
        type="text"
        defaultValue=""
        value={this.props.email}
        onChange={(e) => this.props.updateState({email: e.target.value})}
        placeholder="Email..."
    />
    <Button
        btnType="Submit"
        form="form"
        type='submit'>
        Submit
    </Button>
    <label onClick={props.reset}>Reset fields</label>
</form> );

答案 1 :(得分:1)

您需要通过传递存储在state中的值来控制输入,然后只需重置状态值,然后重置组件值即可。

在下面检查此示例

handleInputChange = (e) => {
    let { name, value } = e.target;
    this.setState({
      ...this.state,
      inputs: {
      [name]: value
      }
});

}

您的组件现在看起来像

<input name='fullName' value={this.state.inputs.fullName} onChange={this.handleInputChange} />

您的重置功能将清除状态,并且您的输入字段为空,因为它是通过state控制的

resetInputFields = () => {
   this.setState({ inputs: {} })
}

答案 2 :(得分:-1)

onHandleFormSubmit = (e) =>{  
    e.preventDefault();
    e.target.reset();
}