我创建了以下组件来在 UnForm 中选择日期:
export default function DatePickerInput({ name, ...rest }) {
const datepickerRef = useRef(null);
const { fieldName, defaultValue = '', registerField } = useField(name);
const [date, setDate] = useState(defaultValue || null);
useEffect(() => {
registerField({
name: fieldName,
ref: datepickerRef.current,
path: 'props.selected',
});
}, [fieldName, registerField]);
return (
<Label htmlFor={fieldName}>
<UnInput>
<ReactDatePicker
ref={datepickerRef}
selected={date}
onChange={setDate}
dateFormat="dd/MM/yyyy"
placeholderText="dd/mm/aaaa"
writable="true"
{...rest}
/>
</UnInput>
</Label>
);
}
Run Code Online (Sandbox Code Playgroud)
为了保存记录,组件正常工作,加载并保存我选择的日期。当我要编辑记录时,尝试在初始加载中加载日期时,页面损坏并显示以下错误:
Unhandled Rejection (TypeError): Cannot assign to read only property 'selected' of object '#<Object>'
Run Code Online (Sandbox Code Playgroud)
如果我注释掉该行path: …
我正在尝试访问存储在ViewBag中的列表的元素,如下所示:
function equipamentoTemControle() {
for(i = 0; i < @ViewBag.qtdEquipamentos; i++) {
var contratocod = @ViewBag.DadosEquipamentos[i].contratocod;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,试图访问时contratocod属性索引的我在Visual Studio说,可变我不存在.我如何访问?