getSnapshotBeforeUpdate method
ReactJS lifecycle method that is invoked immediately after re-rendering when new props and/or state values are committed.
This method is not called for the initial render.
Use this as an opportunity to perform preparation before an update occurs.
Example:
@override
getSnapshotBeforeUpdate(Map prevProps, Map prevState) {
// Previous props / state can be analyzed and compared to this.props or
// this.state, allowing the opportunity perform decision logic.
// Are we adding new items to the list?
// Capture the scroll position so we can adjust scroll later.
if (prevProps.list.length < props.list.length) {
// The return value from getSnapshotBeforeUpdate is passed into
// componentDidUpdate's third parameter (which is new to React 16).
final list = _listRef;
return list.scrollHeight - list.scrollTop;
}
return null;
}
@override
componentDidUpdate(Map prevProps, Map prevState, dynamic snapshot) {
// If we have a snapshot value, we've just added new items.
// Adjust scroll so these new items don't push the old ones out of view.
//
// (snapshot here is the value returned from getSnapshotBeforeUpdate)
if (snapshot !== null) {
final list = _listRef;
list.scrollTop = list.scrollHeight - snapshot;
}
}
See: reactjs.org/docs/react-component.html#getsnapshotbeforeupdate
Implementation
dynamic getSnapshotBeforeUpdate(Map prevProps, Map prevState) {}