Share
Embed
Redux Containers Demystified
A look into react-redux's connect function and how it decides if your connected component should re-render or not.
Mike Klemarewski
January 25, 2018
Transcript
41 slides
-
AGENDA - Build an app that re-renders components when it
shouldn't - Gain an understanding of when connected components re-render - Fix our bug! -
TASK > Build a car configurator > Able to customize
anything about the car > Torsla is fun. Use lots of color! -
CONTAINER function mapStateToProps(state) { return { count: state.wheels.get('count'), price: getPriceForWheels(state),
} } export default connect(mapStateToProps)(Option); -
GET PRICE FUNCTIONS function getPriceForWheels(state) { return { currency: state.currency.get('value'),
total: state.wheels.get('count') * PRICE_PER_WHEEL, } } -
AGENDA - Build an app that re-renders components when it
shouldn't - Gain an understanding of when connected components re-render - Fix our bug! -
CONNECTED COMPONENT - Every time the redux store changes: -
Check some stuff to see if we need to render -
REACT-REDUX SOURCE CODE function handleSubsequentCalls(nextState, nextOwnProps) { const propsChanged =
!areOwnPropsEqual(nextOwnProps, ownProps) const stateChanged = !areStatesEqual(nextState, state) state = nextState ownProps = nextOwnProps if (propsChanged && stateChanged) return handleNewPropsAndNewState() if (propsChanged) return handleNewProps() if (stateChanged) return handleNewState() return mergedProps } -
REACT-REDUX SOURCE CODE function handleSubsequentCalls(nextState, nextOwnProps) { const propsChanged =
!areOwnPropsEqual(nextOwnProps, ownProps) const stateChanged = !areStatesEqual(nextState, state) state = nextState ownProps = nextOwnProps if (propsChanged && stateChanged) return handleNewPropsAndNewState() if (propsChanged) return handleNewProps() if (stateChanged) return handleNewState() return mergedProps } -
CONNECTED COMPONENT - Every time the store changes: - Run
the selector - ownProps change: re-render -
REACT-REDUX SOURCE CODE function handleSubsequentCalls(nextState, nextOwnProps) { const propsChanged =
!areOwnPropsEqual(nextOwnProps, ownProps) const stateChanged = !areStatesEqual(nextState, state) state = nextState ownProps = nextOwnProps if (propsChanged && stateChanged) return handleNewPropsAndNewState() if (propsChanged) return handleNewProps() if (stateChanged) return handleNewState() return mergedProps } -
REACT-REDUX SOURCE CODE function handleNewState() { const nextStateProps = mapStateToProps(state,
ownProps) const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) stateProps = nextStateProps if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) return mergedProps } -
REACT-REDUX SOURCE CODE function handleNewState() { const nextStateProps = mapStateToProps(state,
ownProps) const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) stateProps = nextStateProps if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) return mergedProps } -
OUR CONTAINER function mapStateToProps(state) { return { count: state.wheels.get('count'), price:
getPriceForWheels(state), } } -
REACT-REDUX SOURCE CODE function handleNewState() { const nextStateProps = mapStateToProps(state,
ownProps) const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) stateProps = nextStateProps if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) return mergedProps } -
REACT-REDUX SOURCE CODE function handleNewState() { const nextStateProps = mapStateToProps(state,
ownProps) const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) stateProps = nextStateProps if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) return mergedProps } -
REACT-REDUX SOURCE CODE function handleNewState() { const nextStateProps = mapStateToProps(state,
ownProps) const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) stateProps = nextStateProps if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) return mergedProps } -
REACT-REDUX SOURCE CODE function handleNewState() { const nextStateProps = mapStateToProps(state,
ownProps) const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps) stateProps = nextStateProps if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps) return mergedProps } -
CONNECTED COMPONENT - Every time the store changes: - Run
the selector - ownProps change: re-render - stateProps change: re-render -
CONNECTED COMPONENT - Every time the store changes: - Run
the selector - ownProps change: re-render - !areStatePropsEqual(nextStateProps, stateProps) -
areStatePropsEqual -> shallowEqual - Checks that each object has the
same number of keys - Checks that each key is === to the previous value -
=== FALSE { a: 'a' } === { a: 'a'
} [1, 2, 3] === [1, 2, 3] -
=== For objects, === checks that the references are the
same const a = { a: 'a' } a === { a: 'a' } // false a === a // true -
OUR MAP STATE TO PROPS function mapStateToProps(state) { return {
count: state.wheels.get('count'), price: getPriceForWheels(state), } } -
OUR MAP STATE TO PROPS function mapStateToProps(state) { return {
count: state.wheels.get('count'), price: getPriceForWheels(state), } } -
OUR MAP STATE TO PROPS function mapStateToProps(state) { return {
count: state.wheels.get('count'), price: getPriceForWheels(state), } } > returns an object! > shallow equal check in the connect function fails > component re-renders! -
AGENDA - Build an app that re-renders components when it
shouldn't - Gain an understanding of when connected components re-render - Fix our bug! -
WE COULD ONLY RETURN PRIMITIVES function mapStateToProps(state) { const {
price, currency } = getPriceForWheels(state); return { price, currency, count: state.seatbelts.get('count'), } } -
WE CAN MEMOIZE OBJECT REFERENCES > return the same object
reference if it has not changed > Reselect can help with this > same reference returned, container does not re-render -
TO SUMMARIZE when redux store changes, for each connected component:
run mapStateToProps for that component, shallow compare it's return value to the last value if it changed: re-render