akiran / akiran/react-slick

Generated keys causing issues, and custom keys are no longer working.

未关闭
#2,005 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
11.9k
派生
2.1k
PR 合并指标
30 天内没有已合并 PR

描述

Currently `react-slick` generates keys for internal elements based on the slide index. This causes issues when the slides need to change, like when new slides need to be loaded in (for more details on these issues see [the react-docs](https://reactjs.org/docs/lists-and-keys.html#keys) and [a linked article](https://robinpokorny.medium.com/index-as-a-key-is-an-anti-pattern-e0349aece318)).

This issue was already addressed, in [a PR that allowed react-slick to use any keys that were already defined on child components](https://github.com/akiran/react-slick/issues/279), but it looks like there's been a regression because that fix no longer works. 🙁

**Replicated in CodeSandbox: https://codesandbox.io/s/react-slick-playground-forked-5r00c?file=/index.js**

## Possible Fix?

I made a quick and dirty code change [in `slider.js`](https://github.com/akiran/react-slick/blob/5fb7ac78321f3469118a974af3852742b64fcbd3/src/slider.js#L176) and confirmed that the issue went away when I appended any existing custom keys to the generated ones:

```diff
row.push(
React.cloneElement(children[k], {
- key: 100 * i + 10 * j + k,
+ key: `${100 * i + 10 * j + k}${children[k].key || ''}`,
tabIndex: -1,
style: {
width: `${100 / settings.slidesPerRow}%`,
display: "inline-block"
}
})
);
```

I'm not sure if this change would cover all situations (without impacting other use-cases), but I leave it here to demonstrate that the solution is probably fairly straightforward.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。