akiran / akiran/react-slick

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

Aperta
#2,005 0 commenti 1 reazione 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
11.9k
Fork
2.1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.