I just started to studying React.
When learning LifeCycle and I wonder about componentDidUpdate
method.
As I read componentDidUpdate
method can have three params (prevProps, prevState, snapshot
). If I can find out prevState
from componentDidUpdate
what is the snapshot
for?
It may not important but I just got curiousđ¤
From the componentDidUpdate
docs:
If your component implements the
getSnapshotBeforeUpdate()
lifecycle (which is rare), the value it returns will be passed as a third âsnapshotâ parameter tocomponentDidUpdate()
. Otherwise this parameter will be undefined.
getSnapshotBeforeUpdate()
is invoked right before the most recently rendered output is committed to e.g. the DOM. It enables your component to capture some information from the DOM (e.g. scroll position) before it is potentially changed. Any value returned by this lifecycle will be passed as a parameter tocomponentDidUpdate()
.This use case is not common, but it may occur in UIs like a chat thread that need to handle scroll position in a special way.
A snapshot value (or
null
) should be returned.
Use-cases appear mostly related to querying the DOM, or anything that can't be derived from state
or props
.