By Kamil Grabek
class MyComponent extends React.Component {
render() {
console.log('Rendering MyComponent');
return (
<div>...</div>
);
}
}
<div>
{invitations.map(invitation => (
<div
key={invitation.id}
<div>{invitation.number}</div>
...
/>
))}
</div>
<div>
{invitations.map(invitation => (
<Invitation
key={invitation.id}
data={invitation}
onUpdate={this.handleUpdate}
/>
))}
</div>
class MyComponent extends React.Component {
shouldComponentUpdate(nextPros, nextState, nextContext) {
const hasPropsChanged = shallowEqual(
nextProps,
this.props);
const hasStateChanged = shallowEqual(
nextState,
this.state);
return !hasPropsChanged && !hasStateChanged;
}
}
// class style
class MyComponent extends React.PureComponent {
render() {
...
}
}
// or with functional components
const MyComponent = memo(props => (...));
import produce from "immer"
const baseState = [{
fruit: "Banana",
favourite: false,
}];
const nextState = produce(baseState, draftState => {
draftState.push({ fruit: "Tweet about it" });
draftState[1].favourite = true;
});
const baseState = Immutable.List.of({
fruit: "Try immer",
favourite: false,
});
const nextState = list.push({
fruit: "Banana",
favourite: false,
});
const expensiveCalc = length =>
Array.from({ length }, (_v, k) => k).map(expensiveCalc);
// inside functional component
const { calculationsCost } = props;
const work = useMemo(() => {
expensiveCalc(calculationsCost);
}, [calculationsCost]);
import moize from 'moize';
const method = (a, b) => {
return a + b;
};
const memoized = moize(method);
memoized(2, 4); // 6
memoized(2, 4); // 6, pulled from cache
import moize from 'moize';
const Foo = ({bar, baz}) => {
return (
<div>{bar} {baz}</div>
);
};
export default moize.react(Foo);