Skip to content

Commit

Permalink
fix: Space duplicated key warning
Browse files Browse the repository at this point in the history
close #35305
  • Loading branch information
afc163 committed Apr 28, 2022
1 parent 24fa6a1 commit 8ab756a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
21 changes: 21 additions & 0 deletions components/space/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,25 @@ describe('Space', () => {

expect(wrapper.render()).toMatchSnapshot();
});

// https://github.com/ant-design/ant-design/issues/35305
it('should not throw duplicated key warning', () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined);
mount(
<Space>
<div key="1" />
<div />
<div key="3" />
<div />
</Space>,
);
// eslint-disable-next-line no-console
expect(console.error).not.toHaveBeenCalledWith(
expect.stringContaining('Encountered two children with the same key'),
expect.anything(),
expect.anything(),
);
// eslint-disable-next-line no-console
console.error.mockRestore();
});
});
5 changes: 4 additions & 1 deletion components/space/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ const Space: React.FC<SpaceProps> = props => {
}

const keyOfChild = child && child.key;
// Add `-space-item` as suffix in case simple key string trigger duplicated key warning
// https://github.com/ant-design/ant-design/issues/35305
const defaultKey = `${i}-space-item`;

return (
<Item
className={itemClassName}
key={`${itemClassName}-${keyOfChild || i}`}
key={`${itemClassName}-${keyOfChild || defaultKey}`}
direction={direction}
index={i}
marginDirection={marginDirection}
Expand Down

0 comments on commit 8ab756a

Please sign in to comment.