react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

Infinite loop if initialValue is defined in a SelectArrayInput which is integrated in a formdataconsumer

Open nicodmf opened this issue 4 years ago • 8 comments

What you were expecting: Defined initial values for SelectArrayInput in a FormDataConsumer and have a working environment

What happened instead: An infinite loop take place and stop application.

Steps to reproduce:

  • Go to https://codesandbox.io/s/sweet-darkness-15q1m?file=/src/posts.js:1446-1462
  • Open your browser inspector
  • Go in the integrated browser to post/create
  • You should see "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 the console
  • Choose nom 2 in the first select
  • You should see an infinite loop and real browser (firefox at least) ask to stop the process

Related code:

  • Go to https://codesandbox.io/s/sweet-darkness-15q1m?file=/src/posts.js:1446-1462
<SimpleForm>
      <FormDataConsumer>
        {({ formData, ...rest }) => {
          return (
            <SelectArrayInput
              label="User"
              source="userId"
              initialValue={[1]}
              choices={[
                { id: 1, name: "nom1" },
                { id: 2, name: "nom2" },
                { id: 3, name: "nom3" },
                { id: 4, name: "nom4" }
              ]}
            >
              <ChipField source="name" />
            </SelectArrayInput>
          );
        }}
      </FormDataConsumer>
      {/*...*/}
</SimpleForm>

Other information:

Environment

  • React-admin version: 3.10.2
  • React version: 17.0.1
  • Browser: firefox & chrome tested
  • Stack trace (in case of a JS error):

nicodmf avatar Nov 26 '20 14:11 nicodmf

I've tested you code, and fix it, this is working

 <Create {...props}>
    <SimpleForm>
      <SelectArrayInput
        label="User"
        source="userId"
        initialValue={[1]}
        choices={[
          { id: 1, name: "nom1" },
          { id: 2, name: "nom2" },
          { id: 3, name: "nom3" },
          { id: 4, name: "nom4" }
        ]}
      >
        <FormDataConsumer>
          {({ getSource, ...rest }) => <ChipField source={getSource("name")} />}
        </FormDataConsumer>
      </SelectArrayInput>
      <TextInput source="title" />
      <TextInput multiline source="body" />
    </SimpleForm>
  </Create>

See here https://codesandbox.io/s/condescending-hopper-31heb?file=/src/posts.js

etienne-bondot avatar Nov 26 '20 23:11 etienne-bondot

Thanks @etienne-bondot !

fzaninotto avatar Nov 27 '20 17:11 fzaninotto

Hi @etienne-bondot, the fix proposed does not seems to work when you do need to have the SelectArrayInput as a child of a FormDataConsumer, e.g:

<SimpleForm>
      <FormDataConsumer>
        {({ formData, ...rest }) => {

            formData.someValue ? <SelectArrayInput
              label="User"
              source="userId"
              initialValue={[1]}
              choices={[
                { id: 1, name: "nom1" },
                { id: 2, name: "nom2" },
                { id: 3, name: "nom3" },
                { id: 4, name: "nom4" }
              ]}
            />
       : null // or some other stuff
        }}
      </FormDataConsumer>
      {/*...*/}
</SimpleForm>

The browser will crash as described by @nicodmf, only happens when the initialValue is assigned to that Input.

pflima92 avatar Jan 29 '21 19:01 pflima92

I'm still experiencing this bug as well as of [email protected], why is it closed?

Nunobpinto avatar Jun 28 '21 15:06 Nunobpinto

@Nunobpinto Can you please provide a sandbox showing the issue ?

djhi avatar Jun 28 '21 15:06 djhi

Nevermind, the original sandbox still shows the issue indeed.

djhi avatar Jun 28 '21 15:06 djhi

@pflima92 Thank you for pointing out the initialValue part, It saved my day :)

razorsharpshady avatar Jan 17 '22 20:01 razorsharpshady

UPDATE: I can't reproduce this in version 4 of react-admin anymore

WiXSL avatar Mar 24 '22 19:03 WiXSL