Unform: parseFloat dinamically on component

Created on 20 Aug 2020  路  3Comments  路  Source: unform/unform

I have a lib that returns me an float formatted string (ex: "4458.76") and i want to parse it to Float everytime the input ref is updated.

I tried use:

useEffect(() => { registerField({ name: fieldName, ref: inputRef.current, path: parseFloat('state.numAsString'), }); }, [fieldName, registerField]);

But everytime it return NaN
Someone can help me?

question or discussion

All 3 comments

Hey @medeiroshudson,

The path is where unform look for a value on your component when form is submitted. Usually path is value. And in your registerField you provide path like a parseFloat of a string that's return NaN.

I didn't understood much your use case, but:

  • i think you could register a function in onChange of your component and use parseFloat every time that value change
  • OR
  • if u only need parse to float when form's submitted maybe you could use parseFloat inside onSubmit.

Describe more ur use case. Or if you could put this scenario on codesandbox and share with us, would be better. That's way someone could suggest a better solution.

Hey @medeiroshudson, since you need to return a float value, you can use the getValue method inside registerField.

I created this example for you. Hope it helps!

ps: when you have a getValue, you don't need a path;

Thanks @jpedroschmitz
It fits perfectly nice in my needs!

Was this page helpful?
0 / 5 - 0 ratings