akiran / akiran/react-slick

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

Ouverte
#2,005 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub
Langage dominant
JavaScript
Étoiles
11.9k
Forks
2.1k
Métriques de merge des PR
Aucune PR mergée en 30 j

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.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.