Implementing fields with selection dependent options and also loading selections from an API #10534
-
Hi all, I am essentially trying to implement what is mentioned here: https://github.com/orgs/react-hook-form/discussions/2508, creating Country and State fields, and setting state options based on the Country selection. I am also loading previous selections from an API, and am clearing the State selection if the Country selection changes. The issue I am having is that if I load data from my API, it is being detected as a "change", and the State field is being cleared on initial render. I'm detecting changes with
Reproduced the issue in code sandbox here: https://codesandbox.io/s/agitated-paper-47qmvk?file=/src/App.js Any suggestions on how I can resolve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can simplify the clear state handling by clear only for Working demo: https://codesandbox.io/s/heuristic-blackwell-r8cwcy?file=/src/App.js |
Beta Was this translation helpful? Give feedback.
@alainacook
You can simplify the clear state handling by clear only for
onChange
ofcountry
field.Working demo: https://codesandbox.io/s/heuristic-blackwell-r8cwcy?file=/src/App.js