Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mentions: when mention list has same name,and the value is set as the name, then there will have a key error #18435

Closed
1 task done
ILuZhangKun opened this issue Aug 23, 2019 · 4 comments
Assignees

Comments

@ILuZhangKun
Copy link

ILuZhangKun commented Aug 23, 2019

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

https://wk04r016q8.csb.app/

Steps to reproduce

import React from "react";
import ReactDOM from "react-dom";
import { version, Button, Mentions } from "antd";
import "antd/dist/antd.css";
import "./index.css";


const { Option } = Mentions;
const users = [
  { userName: 'Jessica', useId: '1' },
  { userName: 'Jessica', useId: '2' }
];
ReactDOM.render(
  <div className="App">
    <h1>Please fork this codesandbox to reproduce your issue.</h1>
    <div>Current antd version: {version}</div>
    <div style={{ marginTop: "16px" }}>
      <Mentions
        rows='3'
        style={{ width: '100%' }}
      >
        {users && users.map((item) => {
          return (
            <Option key={`user-${item.userId}`} value={item.userName} className='avatar'>
              <span id={item.userId}>{item.userName}</span>
            </Option>
          );
        })}
      </Mentions>
      <Button type="primary">Example button</Button>
    </div>
  </div>,
  document.getElementById("root")
);

报警示:
【Warning: Encountered two children with the same key, .0:$Jessica.】
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.

What is expected?

应该考虑到被@名单中有重名的情况。

What is actually happening?

实际上直接将value传入Mention中的组件,作为key了。

Environment Info
antd 3.22.0
React 16.3
System 3.22.0
Browser 76.0.3809.100
@ILuZhangKun
Copy link
Author

ILuZhangKun commented Aug 23, 2019

在rc-Mentions中,
https://github.com/react-component/mentions/blob/master/src/DropdownMenu.tsx

<MenuItem
    key={value}
    disabled={disabled}
    className={className}
    style={style}
    onMouseEnter={() => {
        setActiveIndex(index);
    }}
>
    {children}
</MenuItem>

value直接作为key了,没有考虑value相同(即@列表重名)的情况。

@afc163
Copy link
Member

afc163 commented Aug 26, 2019

重名的意义是?

@zombieJ
Copy link
Member

zombieJ commented Aug 26, 2019

value 不能相同,你的示例代码看 value 应该是 userId。Mentions 可以看做是一个带提示的 TextArea,如果你的 value 相同,是没法知道来自哪个 option 的。

@zombieJ zombieJ closed this as completed Aug 26, 2019
@isJaakko
Copy link

重名的意义是?

比如说渲染一个 username 列表(username 有重复),我希望把 @${username} 作为 value 显示在输入框内,如果把 username 设置为 value,就会重复。

这个根本的需求在于:是否可以自定义渲染在输入框中的内容,而不只是默认显示 value 中的内容

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants