I have a form with a couple of conditionally rendered fields. The form is made up of MUI components, react-hook-form and yup for its validation.
Additionally, I have added a console.log()
within the AutocompleteCoffee
, RadioBtnGroup
, TxtField
components that will execute every time the components are rendered.
Scenario
When the page loads you can see a log from each component. Nothing new here.
When you select "Yes" from, Do you like coffee? a new field will be rendered. This action triggers a rerender of all the components on the page.
I am using the watch
method from react-hook-form to keep track of the question mentioned above.
const coffee = watch("coffee", "No");
...
{coffee === "Yes" ? (
<AutocompleteCoffee
required
fullWidth
name="coffeType"
label="Which coffee type"
control={control}
options={coffeList}
error={!!errors.coffeType}
helperText={errors?.coffeType?.message}
/>
) : null}
...
You can see the working CodeSandbox here.
Question
I was wondering how to prevent all the wasted renders. Any ideas?
Thank you in advance!
Via Active questions tagged javascript - Stack Overflow https://ift.tt/pNBYJOw
Comments
Post a Comment