set-state-compare
Version:
setState for React that compares with the current state and only sets the state if changed.
22 lines (16 loc) • 572 B
JavaScript
import {simpleObjectValuesDifferent} from "./diff-utils.js"
export default function shouldComponentUpdate(component, nextProps, nextState) {
if (Object.keys(nextProps).length != Object.keys(component.props).length) {
return true
}
if (component.state && Object.keys(nextState).length != Object.keys(component.state).length) {
return true
}
if (simpleObjectValuesDifferent(nextProps, component.props)) {
return true
}
if (nextState && !component.state) {
return true
}
return simpleObjectValuesDifferent(nextState, component.state)
}