using shouldComponentUpdate instead of getSnapshotBeforeUpdate
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 11.8k
- Forks
- 7.9k
- Ø Merge
- 1 T. 11 Std.
- Gemergte PRs (30 T.)
- 11
Beschreibung
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?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue stellt eine Inline-TypeScript-ScrollLock-Komponente bereit, statt Repository-Dateien oder Tests zu benennen. Beginne mit der Überprüfung der React-Lebenszyklusdokumentation zu shouldComponentUpdate, getSnapshotBeforeUpdate und componentDidUpdate; abgeschlossen ist die Aufgabe, wenn der korrekte Lebenszyklusansatz für diesen Fall der Scroll-Sperre festgelegt und dokumentiert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react, typescript
- Bereich
- documentation, frontend
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100