using shouldComponentUpdate instead of getSnapshotBeforeUpdate
Personne n'a encore pris cette issue.
- Langage dominant
- JavaScript
- Étoiles
- 11.8k
- Forks
- 7.9k
- Merge moyen
- 1 j 11 h
- PR mergées (30 j)
- 11
Description
Hi, I'm struggling a little with the new lifecycles. I have this component which passes down a style which will lock the scrollbar on the window without shifting content when applied on my root App element.
interface ScrollLockProps {
locked: boolean,
children: (style?: CSSProperties) => React.ReactNode
}
export class ScrollLock extends React.Component<ScrollLockProps> {
private scrollY = 0
public shouldComponentUpdate() {
if (!this.props.locked) {
this.scrollY = window.scrollY
}
return true
}
public componentDidUpdate() {
if (!this.props.locked) {
window.scrollTo(0, this.scrollY)
}
}
public render() {
const { locked, children } = this.props
const style: CSSProperties | undefined = locked ? {
position: "fixed",
left: "0px",
right: "0px",
top: `-${this.scrollY}px`,
} : undefined
return children(style)
}
}
The problem is I have to use shouldComponentUpdate to retrieve the scroll position, and this feels wrong to me as this lifecycle surely should only return a boolean and not have any side effects?
I can't use getSnapshotBeforeUpdate because render is called before it, which means the resulting style will be incorrect.
And of course I cannot use componentWillReceiveProps because it is deprecated.
What is the correct way to go about this?
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
L’issue fournit un composant ScrollLock TypeScript inline au lieu de nommer les fichiers ou les tests du dépôt. Commencez par examiner la documentation du cycle de vie de React concernant shouldComponentUpdate, getSnapshotBeforeUpdate et componentDidUpdate ; le travail est terminé lorsque l’approche correcte du cycle de vie pour ce cas de verrouillage du défilement est établie et documentée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, react, typescript
- Domaine
- documentation, frontend
- Type d'issue
- Documentation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100