let memoizeState = null
function mapStateToProps(state) {
const {a, b, c} = state
if (!memoizeState) {
memoizeState = {
a,
b,
c,
fab: f(a,b),
hbc: h(b,c),
gac: g(a,c),
uabc: u(a, b, c)
}
} else {
if (!(a === memoizeState.a && b === memoizeState.b) ) {
// f should invoke
memoizeState.fab = f(a, b)
}
if (!(b === memoizeState.b && c === memoizeState.c) ) {
// h should invoke
memoizeState.hbc = h(b, c)
}
if (!(a === memoizeState.a && c === memoizeState.c) ) {
// g should invoke
memoizeState.gac = g(a, c)
}
if (!(a === memoizeState.a && b === memoizeState.b && c === memoizeState.c) ) {
// u should invoke
memoizeState.uabc = u(a, b, c)
}
memoizeState.a = a
memoizeState.b = b
memoizeState.c = c
}
return memoizeState
}
避免不必要的重复计算
参考: https://juejin.im/post/5a04825b6fb9a0450f216459