0

I have a stateful component that calls a CEP promise, to fetch data from post offices. This data is fetched when the Zip input is fulfilled with 9 chars - 8 number and an '-' - and return an object with desired information.

Heres the function:

  const handleZipCode = useCallback(
    async ({ target }: ChangeEvent<HTMLInputElement>) => {
      const { value } = target;

      try {
        if (value.length === 9 && isDigit(value[8])) {
          const zip = await cep(value);

          if (zip?.city) {
            setZipData(zip);
          } else
            addressFormRef.current?.setErrors({
              user_zip_code: 'CEP not found',
            });
        }
      } catch (e) {
        addressFormRef.current?.setErrors({
          user_zip_code: e.message ?? 'CEP not found',
        });
      }
    },
    [isDigit]
  );

Then, on the return I have some fields, example:

            <fieldset>
              <legend>Address</legend>
              <Input
                mask=''
                name='user_address'
                placeholder='Rua um dois três'
                defaultValue={zipData.street}
              />
            </fieldset>

Here's the Input component:

const Input: React.FC<InputProps> = ({ name, ...rest }) => {
  const { fieldName, defaultValue, registerField, error } = useField(name);
  const inputRef = useRef(null);

  useEffect(() => {
    registerField({
      name: fieldName,
      ref: inputRef.current,
      path: 'value',
      // eslint-disable-next-line
      setValue(ref: any, value: string) {
        ref.setInputValue(value);
      },
      // eslint-disable-next-line
      clearValue(ref: any) {
        ref.setInputValue('');
      },
    });
  }, [fieldName, registerField]);

  return (
    <Container>
      <ReactInputMask ref={inputRef} defaultValue={defaultValue} {...rest} />
      {error && <Error>{error}</Error>}
    </Container>
  );
};

However the zipData seems to update, but the default value is not fulfilled. What I'm doing wrong?

2
  • What is this Input component? did you make it, or is it from a library? Commented Oct 9, 2020 at 12:53
  • Is from a library. Actually I'm using unform and react-input-mask. I editted to add the Input component. Commented Oct 9, 2020 at 12:56

1 Answer 1

2

The default value will not change, as unform is an uncontrolled form library, the defaultValue will be set on the first render of the page and then will not change anymore.

To fix your problem you can do something like:

// on your handleZipCode function
formRef.current.setData({
   zipData: {
     street: zipResult.street,
   },
});
Sign up to request clarification or add additional context in comments.

5 Comments

I got this error: ref.setInputValue is not a function.
Didn't you forget the current?
Yeah, tbh im not a big fan of unform at all, i recommend you reac-hooks-form
Okay, next project I'll use, on this will create a giant extra work to change. I'll search for another way to change the value! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.