I want to implement a form that is made of base fields (e.g.: product name, price and type), then type-specific fields depending on the type selected.
The solution seemed straightfoward at first (see this codesandbox), since I managed to render different components based on the type found in the current product
state.
However, because these components are being rendered conditionally while using hooks internally such as useEffect
, they are breaking the Rules of Hooks and causing the following error when I change from one product type to another:
Should have a queue. This is likely a bug in React. Please file an issue.
Warning: React has detected a change in the order of Hooks called by TypeSpecificForm. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks (...)
Previous render Next render
------------------------------------------------------
1. useState useEffect
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
in TypeSpecificForm (at ProductForm.js:36)
in div (at ProductForm.js:14)
in ProductForm (at App.js:8)
in App (at index.js:15)
in StrictMode (at index.js:14)
What is the correct way to render these partial fields dynamically?