
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-memoize
Advanced tools
7kb MobX-level memoization library, which tracks all the arguments you are really depends on.
Uses memoize-state underneath, providing the same magic for get as immer provided to set.
Works fine in all browsers including IE11.
Just write code as you want. It it will be properly memoized.
This is declarative component memoization for React! Based on Dan Abramov's tweet
Could change the way you did componentWillReceiveProps, could replace getDerivedStateFromProps, could make things better.
IE11+, React 15 and React 16.3 compatible.
Memoize, MemoizedFlow, MemoizeContext accepts one or more functions to select or transform incoming data, and provide result to a function-as-child.
MemoizedRender is memoizing the function-as-child itself.
import Memoize from 'react-memoize';
<Memoize
prop1 = "theKey"
state = {this.state}
// values from above will be provided to compute function
compute={({prop1, state}) => heavyComputation(state[prop1])} // Memoize tracks WHAT you are doing
pure // Memoize will be a pure component itself
>
{ result => <Display>{result}</Display>}
</Memoize>
There is only one prop - compute, all others will be passed inside.
Memoize get compute function, add passes all the other props to it, streaming result to the render prop.
If pure prop is set ReactMemoize wil behave as PureComponent, and not update children when could not.
getDerivedStateFromProps gives you ability to from a new state from props, while componentDidUpdate enables you to react
to the state changes.
MemoizedFlow is getDerivedStateFromState. Following example react to the state changes, allowing
to change ordering of rows and applies a pagination.
"The Flow" is safe and performant way to form something from something, and rebuilt then the time calls.
import {MemoizedFlow} from 'react-memoize';
class SortablePageableTable extends Component {
state = {
page:0,
perPage:10,
filter: I => I
};
onSortChange = (order) => this.setState(order)
onPageChange = page => this.setState(page);
render () {
return (
<MemoizedFlow
input={{...this.props, ...this.state}}
flow={[
// will react on rows or filter change
({rows, filter}) => ({rows: list.filter(filter)}),
// will react on rows(from step1) change or order
({rows, order}) => ({rows: list.slice().sort(order)}), // !! clone array before sort
// will react on rows and pagination changes
({rows, page, perPage}) => ({rows: list.slice(page*perPage, (page+1)*perPage)}),
// will react on something else, not related
({rain, bows}) => ({rainbows: rain+bows, rain: null, bows: null })
]}
>
{output => <Table {...output} onSortChange={this.onSortChange} onPageChange={this.onPageChange}/>}
</MemoizedFlow>
}
}
<SortablePageableTable rows = {tableRows} />
First step is getting input, and each following is reading from a value provided before, spreading
own result over it. Until the last step will be reached, and output will be provided to render prop.
Each step is memoized, as usual, and will always reuse value from the steps before.
React memoize also provides component to select and memoize data from React16 context, or any other component which will pass some values into renderProp.
import {MemoizeContext} from 'react-memoize';
<Context.Provider value={{prop1: 1, prop2: 2, prop3: 3}}>
<MemoizeContext consumer={Context.Consumer} selector={select}>
{values => <Render {...values} />}
</MemoizeContext>
</Context.Provider>
consumer could be any "context"-compatible Component - React.context, create-react-context, unstated, react-copy-write.
All the additional props will be passed down to consumer.
It is better to explain using example.
<MemoizeContext consumer={Consumer} prop1={1} anotherProp={3} selector={select}> />
// will result
<Consumer prop1={1} anotherProp={3}>
{ contextValues => <ReactMemoize {...contextValues} compute={selector}>...</ReactMemoize>}
</Consumer>
This is like Redux without dispatching. State in context, selector aka mapStateToProps, and magic memoization in between.
See it in action -> https://codesandbox.io/s/xjz5y3wzrz 🛠
MemoizedRender is mostly usable with Context API
import {MemoizedRender} from 'react-memoize';
<Context.Provider value={{prop1: 1, prop2: 2, prop3: 3}}>
<MemoizedRender consumer={Context.Consumer}>
{values => <Render {...select(values)} />}
</MemoizedRender>
</Context.Provider>
Or, the better example (from react-copy-write)
const UserAvatar = ({ id }) => (
<MemoizedRender consumer={State.Consumer}>
{state => (
<div className="avatar">
<img src={state.users[id].avatar.src} />
</div>
)}
</MemoizedRender>
);
While react-copy-write declares that _ The problem with this is that whenever any value in state changes, UserAvatar will be re-rendered, even though it's only using a single property from a single, nested object._
This example will work, as long MemoizedRender will track used keys, and perform update only when necessary.
It is also possible to provide value as a prop
<MemoizedRender value={originalValue}>
{values => <Render {...select(values)} />}
</MemoizeContext>
MemoizedRender memoizes "render" as a whole. This is absolute pure component. Be carefull. Might be not 100% compatible with async rendering if you pass values you were provided down the tree, as long async accessed keys are not tracked. Thus - MemoizedRender may not react to changes in them.
React-memoize uses memoize-state underneath to perform MobX like memozation
and achive the maximal minimal level of memoization cache misses. Sounds a bit strange, but this mean - react-memoize will try to preserve the current state at all costs. From 10% to 50% "more" in comparison.
In all the cases only ONE! result is memoized. The goal of the component is to cut off updates.
For example:
<Memoize
state = {this.state}
compute={({state}) => soSomethingWith(state.stateSubKey)}
>
{ result => ....}
</Memoize>
<Memoize
usedProp = {1}
unusedProp = {2}
compute={({usedProp}) => soSomethingWith(usedProp)}
>
{ result => ....}
</Memoize>
<Memoize
list = {this.state.list}
compute={({list}) => list.filter(item => item.value).map(item => item.text)}
>
{ result => ....}
</Memoize>
The same magic as in beautiful-react-redux, kudos to memoize-state library.
PS: For systems without Proxy support memoize will use memoize-one
MIT
FAQs
Memoized mapStateToProps on component level
We found that react-memoize demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.