akiran / akiran/react-slick

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

Open
#2,005 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
11.9k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.