Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

useTypedController remounts component repeatedly #6

Closed
susisu opened this issue Jul 8, 2020 · 3 comments
Closed

useTypedController remounts component repeatedly #6

susisu opened this issue Jul 8, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@susisu
Copy link

susisu commented Jul 8, 2020

Hi,

useTypedController only recreates TypedController component when control is changed:

But actually useForm returns different control object on every render, and TypedController is always recreated.
https://github.com/react-hook-form/react-hook-form/blob/e757653b39645bf9f547525d27f88224d8049539/src/useForm.ts#L1162

This causes the input component to be unmounted and remounted repeatedly, and can impact on both performance and experience.

We are encountering the worst case of it, where the input component triggers rerender of the parent on its initial render using useEffect:

Because TypedController is always "different" component, React remounts the component endlessly with the following warning:

Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
    in TextInput (at App.tsx:23)
    in Controller
    in Unknown (at App.tsx:20)
    in div (at App.tsx:18)
    in App (at src/index.tsx:7)

Thanks.

@bluebill1049 bluebill1049 added the bug Something isn't working label Jul 8, 2020
@bluebill1049
Copy link
Member

I think the easiest way is pretty just cache the control, I will try to fix this issue later today 🙏

@bluebill1049 bluebill1049 self-assigned this Jul 8, 2020
@bluebill1049
Copy link
Member

can you try this beta version: @hookform/strictly-typed@0.0.2-beta.1?

@susisu
Copy link
Author

susisu commented Jul 9, 2020

Thank you for looking into it! It looks working for my case.
I have a question about how it works, and added a comment on your PR #7.

bluebill1049 added a commit that referenced this issue Jul 10, 2020
fix #6 useTypedController remounts component repeatedly
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants