为什么 Material-UI 日期与占位符重叠值并抱怨受控输入?
Why Material-UI date overlaps value with placeholder and complains about controlled input?
这里是 codesandbox.
我想创建一个 DateOnly
组件,我可以在我的项目中轻松使用它。
并且 Material-UI 抱怨说:
Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components
这是显示所选日期的方式:
我知道受控输入意味着你同时绑定value={value}
和onChange={(newValue) => setValue(newValue);}
。
这是我的 return 值:
return (
<div>
<KeyboardDatePicker
error={isValid() ? false : true}
id={id}
label={placeholder}
format="MM/dd/yyyy"
value={currentValue}
onChange={(date) => {
setCurrentValue(date);
}}
KeyboardButtonProps={{
"aria-label": "Change " + placeholder
}}
fullWidth
/>
</div>
);
但我不知道为什么这个日期字段无法识别。
我该如何解决这个问题?
这里是 codesandbox.
我想创建一个 DateOnly
组件,我可以在我的项目中轻松使用它。
并且 Material-UI 抱怨说:
Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components
这是显示所选日期的方式:
我知道受控输入意味着你同时绑定value={value}
和onChange={(newValue) => setValue(newValue);}
。
这是我的 return 值:
return (
<div>
<KeyboardDatePicker
error={isValid() ? false : true}
id={id}
label={placeholder}
format="MM/dd/yyyy"
value={currentValue}
onChange={(date) => {
setCurrentValue(date);
}}
KeyboardButtonProps={{
"aria-label": "Change " + placeholder
}}
fullWidth
/>
</div>
);
但我不知道为什么这个日期字段无法识别。
我该如何解决这个问题?