【发布时间】:2023-03-09 21:21:01
【问题描述】:
到目前为止我有这个,但需要在使用 react-hook-form 和 gatsby-plugin-intl 的现有表单上的箭头函数中使用它
表单应采用以下格式,我也使用 useState 进行提交。
代码正在检查所选选项的值以显示条件字段/我想我需要为此字段使用 watch API,但首先需要将 包含到完整的表单中。
const ContactForm = ({ intl }) => {
const [submitted, setSubmitted] = useState(false)
[...] }
相对
https://codesandbox.io/s/cocky-oskar-o2m6c
class SelectOptions extends React.Component {
constructor(props) {
super(props)
this.state = {
fruit: 'Apple',
}
this.handleChange = this.handleChange.bind(this)
}
handleChange(e) {
this.setState({ fruit: e.target.value })
}
render() {
const isComplaint = this.state.fruit
let complaint
if (isComplaint === 'Strawberry') {
complaint = <div>How Are You Doing?</div>
} else {
complaint = <div></div>
}
return (
<div className='mt-10'>
SELECT ONE
<div className='mt-2'>
<select value={this.state.fruit} onChange={this.handleChange}>
<option value='apple'>Apple</option>
<option value='Strawberry'>Strawberry</option>
<option value='Cucumber'>Cucumber</option>
</select>
</div>
{complaint}
</div>
)
}
}
export default SelectOptions
【问题讨论】:
-
具体是什么问题?
-
当您说需要转换为箭头函数时,您的意思是说您正在尝试将 React 类组件转换为 React 函数组件,以便可以使用 React hooks?函数组件不必是箭头函数,也可以是普通的
function函数。 -
是的。感谢您帮助我澄清 @DrewReese 这正是我想说的。
-
感谢您签入@DaveNewton。我很感激。
标签: reactjs react-functional-component