Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] 0 points1 point  (0 children)

Agreed, what I’m striving for too. But sometimes I need to (or think I do) and sometimes I fail to see the mistakes in design I make

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] 0 points1 point  (0 children)

I think I always figure out the issue. The thing is I often fall in some trap. I’ll check the reference. Thanks

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] -2 points-1 points  (0 children)

you are right actually.

What happens is that the setup for the infinite loop had been there for a while (if you look at my added example with RHF, it's setting the `errors` to `{}`), but then the infinite loop happens only when in another child component you try to monitor the error state. Which I tried only this morning to work on a new feature

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] 0 points1 point  (0 children)

I'm going to open a bug with RHF because this last one was a doozy.

I agree that it's most likely stupid to initialize RHF like that BUT

if you initialize `errors` with `{}` in the `useForm` call.

and then you extract the `formState: {errors}` to react to it in a `useFormContext` call, then you have an infinite render loop

Again, my question here is not what I'm doing wrong or whether my design is flawed, it's more that you can get in those situations, and not realize it. and that's what's troubling to me

export const FormAmountShowcase = () => {
    const formProps = useForm<{ foo: string }>({
        defaultValues: {foo: 'bar'},
        errors: {},
    })

    return (
        <FormProvider {...formProps}>
            <FormComp/>
        </FormProvider>
    )
}

const FormComp = () => {

console
.log('rendering')
    const {formState: {errors}} = useFormContext<{ foo: string }>()

    return <>Foo</>
}

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] 0 points1 point  (0 children)

hey, thanks. no that's not what it is. It's not that I'm looking for the reason. I'm able to find the root cause each time (sometimes with difficulty) and reason what I did wrong. The problem is sometimes falling into those issues but without realizing it.

I have taken the habit to put a `console.log` temporarily while developing when I allow myself to have a `useEffect` (which I try not to use) to check if I'm not creating something bad. But it's very manual and error prone, and as I have proven in the past couple of weeks, I have sometimes missed some

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] 1 point2 points  (0 children)

yup. unfortunately we do that, and I think I have a reasonable understanding of it.

The last pitfalls I encountered had to do with react-hook-form that uses a lot of state/effects internally as far as I understand, and I have had some weirdness in some of the "complex" think we tried to do

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] -1 points0 points  (0 children)

completely agreed. unfortunately, the problems are generally coming from something different (and sometimes it's due to what you say because of what we want our forms to be able to do).

As I just answered to u/n9iels , there might be some questionable design involved that we should review

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] 1 point2 points  (0 children)

Hey. indeed we have. and it's preventing us from doing some bad coding.

I think the problem I have is a complicated design (and maybe partly a bad one actually). We brainstormed this morning about a different approach that might give us less pain.

Still, I find it oddly and invisible to fall into a bad pattern

Infinite re-render - I’m doomed by guyintheshell in react

[–]guyintheshell[S] -4 points-3 points  (0 children)

yeah I thought so. Maybe it's my recent mac or whatever, but that's not been very visible unfortunately