Datepicker component breaking the screen in editing

Asked

Viewed 176 times

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 value true without the quotation marks: writable=true? I’ve seen plugins that deal "true" (in quotes) as a string and true (unquote) as boolean and cause differentiation and incorrect behavior when string.

  • He doesn’t accept.

  • 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 the react-datepicker, but I can’t even say that, since he doesn’t have a prop writable.

  • 1

    @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

2 answers

0

Problem :

formRef.current.setFieldValue('birthday',value) this will attempt to define value in the given path, in our case, provided that path be it props.selected

And props.selected is a read-only property, so you cannot set value in props, hence the error.

useEffect(() => {
    registerField({
      name: fieldName,
      ref: datepickerRef.current,
      path: 'props.selected', // <---- this is props, and it's readonly
      clearValue: (ref) => {
        ref.clear();
      },
    });
}, [fieldName, registerField]);

Solution :

You can remove the path and use the methodsgetter and setter, appointed asgetValue and setValue:

setValue : to define the starting value or past setFieldValue

getValue : to get value when sending

useEffect(() => {
    registerField({
      name: fieldName,
      ref: datepickerRef.current,
      clearValue: ref => {
        ref.clear();
      },
      setValue: (e, v) => {
        setDate(new Date(v)); // <---- Setting up default value 
      },
      getValue: () => {
        return datepickerRef.current.props.selected; // to get selected value from Date picker's props
        // OR
        return Date.toString(); // to get selected value from state it self
      }
    });
}, [fieldName, registerField]);

WORKING DEMO :

Edit #SO-Datepicker-unform-setvalue

  • Any Reason for downvote?

  • 1

    Because this is the Stackoverflow in Portuguese. This community is exclusively for Lusophone users so questions and Answers should be Asked in English. If you want to continue communicating in English, you can use Stackoverflow EN.

  • Okay Thanks man,for Explaining, Let me Convert it into English,

  • Do you need help with that?

  • @Augustovasques,I know Answer is 100% correct but I think,Getting downvotes just for a language, I’ve tried to Convert it, will you Please also check if its still understandable or you can update it, Thanks for asking.

  • it’s OK(it’s OK).

  • @Augustovasques, Thanks man (thanks man) :)

Show 2 more comments

-2

writable="true"

In JSX, to insert a boolean value, among others, you must insert it between ex keys. writable={true},

Browser other questions tagged

You are not signed in. Login or sign up in order to post.