0

I have the next application: https://codesandbox.io/s/suspicious-murdock-87j13?file=/SubForm.js

 <Form.List name="names">
        {(fields, { add, remove }) => {
          return (
            <div>
              {fields.map((field, index) => {
                console.log("m", field);
                return (
                  <Form.Item
                    {...(index === 0
                      ? formItemLayout
                      : formItemLayoutWithOutLabel)}
                    label={index === 0 ? "Passengers" : ""}
                    required={false}
                    key={index}
                  >
                    <Form.Item
                      {...field}
                      name={[field.name, "outer"]}
                      fieldKey={[field.fieldKey, "outer"]}
                      validateTrigger={["onChange", "onBlur"]}
                      rules={[
                        {
                          required: true,
                          whitespace: true,
                          message:
                            "Please input passenger's name or delete this field."
                        }
                      ]}
                      noStyle
                    >
                      <Input
                        placeholder="passenger name"
                        style={{ width: "60%" }}
                      />
                    </Form.Item>
                    <Form.Item
                      {...field}
                      name={[field.name, "outer1"]}
                      fieldKey={[field.fieldKey, "outer1"]}
                      validateTrigger={["onChange", "onBlur"]}
                      rules={[
                        {
                          required: true,
                          whitespace: true,
                          message:
                            "Please input passenger's name or delete this field."
                        }
                      ]}
                      noStyle
                    >
                      <Input
                        placeholder="passenger name"
                        style={{ width: "60%" }}
                      />
                    </Form.Item>
                    <Form.Item>
                      <InnerForm fieldKey={field.key} />
                    </Form.Item>
                    {fields.length > 1 ? (
                      <MinusCircleOutlined
                        className="dynamic-delete-button"
                        style={{ margin: "0 8px" }}
                        onClick={() => {
                          remove(field.name);
                        }}
                      />
                    ) : null}
                  </Form.Item>
                );
              })}
              <Form.Item>
                <Button
                  type="dashed"
                  onClick={() => {
                    add();
                  }}
                  style={{ width: "60%" }}
                >
                  <PlusOutlined /> Add field
                </Button>
              </Form.Item>
            </div>
          );
        }}
      </Form.List>

      <Form.Item>
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>

When user click on Add field button, in console appears the warning:

Warning: Encountered two children with the same key, `0`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Also this warning appears when i click on Add sub-field. I can't figure out why this warning appears.
Question: Why the above warning appears and how to remove it?

9
  • Keys help React identity which items have changed (added/removed/re-ordered). To give a unique identity to every element inside the array, a key is required. So you need to add a unique key for all of the react element rendering in the page. The system should be key={keyName} Commented Jul 3, 2020 at 10:45
  • @MANISHKUMARCHOUDHARY, but now all items have keys, where could be the problem? Commented Jul 3, 2020 at 10:46
  • console.log(index) I think the two keys have same value. We should avoid component with a duplicate key too. The problem should be inside the map(..) function where the key value is getting mapped Commented Jul 3, 2020 at 10:49
  • @MANISHKUMARCHOUDHARY, inside map() every item has key={index}. Why the warning could appear? Commented Jul 3, 2020 at 10:52
  • because the index value will be the same for both the <Form.Item> right? Commented Jul 3, 2020 at 10:56

3 Answers 3

2

You have two Form.Item Component in your code, and they use same key index, try to use different key like 'item1' + index and 'item2' + index.

And, in your index.js file also have same problem.

Here is Demo forked from you: https://codesandbox.io/s/dark-darkness-xz0wf?file=/SubForm.js

Sign up to request clarification or add additional context in comments.

Comments

0

As reactJs primary works on re rendering items every time you make a change the state of that component. It requires an unique key to track each and every item so that re-rendering can be optimized. Not all items needs to be re-render every time.

This warning essentially means that you have multiple items with same key names. In order to fix this you have to provide unique "key" attribute to each child component which is getting updated, components which can potentially trigger an re-render.

2 Comments

,i checked my code and now all elements have keys, but i can't figure out how to change the code to make disappearing the warning, could you help please?
one request first : passing index as key value is an anti-pattern it can cause a lot of problem. Could you please pass some unique values to key instead of 'index' and again check once.
0

you don't need to use multiple times key={index} or key={"g"} use once only.

Please try following

import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Form, Input, Button, Space } from 'antd';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';

const Demo = () => {
  const onFinish = values => {
    console.log('Received values of form:', values);
  };

  return (
    <Form name="dynamic_form_nest_item" onFinish={onFinish} autoComplete="off">
      <Form.List name="users">
        {(fields, { add, remove }) => {
          return (
            <div>
              {fields.map(field => (
                <Space key={field.key} style={{ display: 'flex', marginBottom: 8 }} align="start">
                  <Form.Item
                    {...field}
                    name={[field.name, 'innerName']}
                    fieldKey={[field.fieldKey, 'innerName']}
                    
                  >
                    <Input placeholder="passenger 1" />
                  </Form.Item>
                  <Form.Item
                    {...field}
                    name={[field.name, 'innerName2']}
                    fieldKey={[field.fieldKey, 'innerName2']}
                  >
                    <Input placeholder="passenger 2" />
                  </Form.Item>

                  <MinusCircleOutlined
                    onClick={() => {
                      remove(field.name);
                    }}
                  />
                </Space>
              ))}

              <Form.Item>
                <Button
                  type="dashed"
                  onClick={() => {
                    add();
                  }}
                  block
                >
                  <PlusOutlined /> Add field
                </Button>
              </Form.Item>
            </div>
          );
        }}
      </Form.List>

      <Form.Item>
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>
  );
};

ReactDOM.render(<Demo />, document.getElementById('container'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.