Oscar Tong
Everything that has a beginning has an end.
But I've heard React is fast!
Simple to write, easy to mess up
We can't improve what we don't measure
const ParentComponent = props => (
<div>
<Demo foo={1}/>
</div>
)
<Demo foo={{bar:123}}/>
❌ <Demo foo={this.props.bar || []}/>
✔
const EMPTY_ARR = []; <Demo foo={this.props.bar || EMPTY_ARR}/>
❌
const mapStateToProps = state => ({
bar: getSomeValue(state) || { },
})
✔
const EMPTY_OBJ = {};
const mapStateToProps = state => ({
bar: getSomeValue(state) || EMPTY_OBJ,
})
❌
<Demo onClick={() => doSomething()}/>
✔
const onClick = useCallback(
() => doSomething(),
[foo, bar]
);
<Demo onClick={onClick}/>
The difference between them is that
React.Component doesn’t implement shouldComponentUpdate(),
but React.PureComponentimplements it with a
shallow prop and state comparison.
React components for efficiently rendering
large lists and tabular data
export const getTotalUnreadCount = createSelector(
// dependencies
({ state }) => state.messages.sessions,
({ state }) => state.messages.selectedSession,
({ state }) => state.messages.muted,
({ state }) => state.messages.unreadCounts,
({ state }) => state.onlineStatus.onlineStatus,
({ state }) => state.notification.settings,
// heavy computation
(sessions, selectedSession, muted, ...) => {
return calculateTotalUnreadCount( ... );
}
);
✔Shallow Compare
By Oscar Tong
Just another react-redux performance slides