0
I created the following component to select dates in 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>
);
}
To save records the component is working normally, loading and saving the date I selected. When I edit a record, when trying to load the date in the initial load, the page is broken and the following error is displayed:
Unhandled Rejection (TypeError): Cannot assign to read only property 'selected' of object '#<Object>'
If I comment on the line path: 'props.selected'
, in the useEfect()
the screen is not broken, but the date is not filled in the component.
How to make it work ?
I don’t know about that, but you’ve tried it
writable="true"
with the valuetrue
without the quotation marks:writable=true
? I’ve seen plugins that deal"true"
(in quotes) as a string andtrue
(unquote) as boolean and cause differentiation and incorrect behavior when string.– Sam
He doesn’t accept.
– Raphael Prado de Oliveira
Without an example reproducible it is not possible to help. You have not shown nor the
import
's for someone to try to reproduce. It seems that you are using thereact-datepicker
, but I can’t even say that, since he doesn’t have a propwritable
.– Rafael Tavares
@Rafaeltavares yes. I’m using the React-datepicker. I posted the complete codes here in these two Gists: https://gist.github.com/raphaelpradoo/aa301509820d78b726ff8ac258e750a1 https://gist.github.com/raphaelpradoo/ed20a035777a37bdc5d370f2fef699c9
– Raphael Prado de Oliveira