Files
react.dev/examples/update-on-async-rendering/updating-state-from-props-after.js
2018-05-24 18:18:04 -07:00

24 lines
545 B
JavaScript

// After
class ExampleComponent extends React.Component {
// Initialize state in constructor,
// Or with a property initializer.
// highlight-range{1-4}
state = {
isScrollingDown: false,
lastRow: null,
};
// highlight-range{1-7}
static getDerivedStateFromProps(props, state) {
if (props.currentRow !== state.lastRow) {
return {
isScrollingDown: props.currentRow > state.lastRow,
lastRow: props.currentRow,
};
}
// Return null to indicate no change to state.
return null;
}
}