marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
24.89k stars 5.23k forks source link

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

Closed nicodmf closed 3 months ago

nicodmf commented 3 years ago

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:

Related code:

<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

etienne-bondot commented 3 years ago

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

fzaninotto commented 3 years ago

Thanks @etienne-bondot !

pflima92 commented 3 years ago

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.

Nunobpinto commented 3 years ago

I'm still experiencing this bug as well as of react-admin@3.16.4, why is it closed?

djhi commented 3 years ago

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

djhi commented 3 years ago

Nevermind, the original sandbox still shows the issue indeed.

razorsharpshady commented 2 years ago

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

WiXSL commented 2 years ago

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

fzaninotto commented 3 months ago

With the release of react-admin v5, react-admin v3 has reached its end of life. We won't fix bugs or make any new release on the 3.x branch. We recommend that you switch to a more recent version of react-admin.

So I'm closing this issue as we won't fix it.