akiran / akiran/react-slick

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

Offen
#2,005 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
11.9k
Forks
2.1k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.