我有以下示例代码Form
,并提供了初始数据。 如果我重构以Class component
预先填充的表单,但是当我使用钩子时,没有任何结果。
const ActivitiesForm = ({ goBackButton }) => {
const { items, initialValues } = useSelector((state) => {
return {
initialValues: state.data,
items: state.items,
}
})
const [activityName, setActivityName] = useState(
initialValues ? initialValues.activityDescription : ''
)
const handleInputName = (e) => setActivityName(e.target.value)
return (
<div>
<Form
onSubmit={handleSubmit(handleSubmit)}
className='ui form'
>
<Field
component={ConstructField('input')}
onChange={handleInputName}
label='Activity Name'
name='activityName'
placeholder='Activity Name'
/>
<Button
negative
onClick={() => handleSubmit_('del')}
>
Delete the Youth
</Button>
</Form>
</div>
)
}
const activityform = reduxForm({
form: 'activityform',
enableReinitialize: true,
})(ActivitiesForm)
export default activityform
尝试像这样使用useEffect
钩子:
useEffect(() => {
if(initialValues){
setActivityName(initialValues.activityDescription);
}
}, [initialValues)