我想进行一次重置以刷新输入的状态。
我在网上进行了13次测试:this.messageForm.reset()
,但出现的错误并非函数
import React, {Component} from 'react'
import {Form, Button} from 'reactstrap'
export default class Formulaire extends Component {
createMessage = event => {
event.preventDefault()
console.log(this.message.value)
const message = this.message.value
this.props.addMessage(message)
//reset
this.messageForm.reset()
}
render() {
return (
<div>
<Form
className='form'
onSubmit={event =>this.createMessage(event)}
ref={input => this.messageForm = input}
>
<textarea
required
maxLength='140'
ref={input => this.message = input}
>
</textarea>
<Button type="submit"> Envoyer! </Button>
</Form>
</div>
)
}
}
答案 0 :(得分:1)
问题在于您的<Form>
不是HTML表单元素,而是reactstrap Form component,因此,它没有.reset()
方法。
Reactstrap允许设置innerRef
属性来访问实际的DOM元素:
<Form
className='form'
onSubmit={event =>this.createMessage(event)}
innerRef={input => this.messageForm = input}
>