我已经知道“值”与受控组件一起使用。但是在这个测试中,当我删除该属性时value = {controlledValue}
,它仍然可以正常工作。
import React, { useState } from "react";
export const Controlled = props => {
const [controlledValue, updateControlledValue] = useState("");
const handleChange = event => {
updateControlledValue(event.target.value);
};
const handleSubmit = event => {
event.preventDefault();
props.handleSubmittedData({
controlled: controlledValue
});
};
return (
<form onSubmit={handleSubmit}>
<h2>Controlled</h2>
<input
name="controlled"
placeholder="controlled"
type="text"
value={controlledValue}
onChange={handleChange}
/>
<button disabled={!controlledValue} type="submit">
Submit
</button>
</form>
);
};
value
当我们已经处理了controlledValue
状态时,拥有财产有什么意义handleChange
?