snapshot 和 prevState,componentDidUpdate 中的 prevProps 有什么不同?

What is different between snapshot and prevState, prevProps from componentDidUpdate in react?

刚开始学React

在学习 LifeCycle 时,我对 componentDidUpdate 方法感到疑惑。

正如我所读 componentDidUpdate 方法可以有三个参数 (prevProps, prevState, snapshot)。如果我能从 componentDidUpdate 中找出 prevState 那么 snapshot 是做什么用的?

这可能不重要,但我只是好奇

来自 componentDidUpdate 文档:

If your component implements the getSnapshotBeforeUpdate() lifecycle (which is rare), the value it returns will be passed as a third “snapshot” parameter to componentDidUpdate(). Otherwise this parameter will be undefined.

getSnapshotBeforeUpdate

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 to componentDidUpdate().

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.

用例似乎主要与查询 DOM 或任何 不能 stateprops 派生的东西有关.