
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-immutable-pure-component
Advanced tools
React PureComponent implementation embracing Immutable.js
Unfortunately React.PureComponent
is not embracing Immutable.js
to it full
potential. While Immutable.js
provides hash value,
witch allows for fast comparison of two different instances
React.PureComonent
is only comparing addresses of those instances.
The ImmutablePureComponent
uses is to compare values and
extends component functionality by introducing:
updateOnProps
updateOnStates
With those properties you can specify list of props or states that will be
checked for changes. If value is undefined
(default) then all props
and
state
will be checked, otherwise array of keys or paths is expected. The path
is an Array
of keys like in the example below. Path values are working for
any mix of supported collection as long as given key exists, otherwise checked
value is undefined
. Immutable.Collection
, plain Objects, Arrays, es6 Map
and any collection providing get
and has
functionality are all supported.
type UpdateOn<T> = Array<$Keys<T> | any[]>;
export class ImmutablePureComponent<
Props,
State = void,
> extends React$Component<Props, State> {
updateOnProps: UpdateOn<Props>;
updateOnStates: UpdateOn<State>;
}
export default ImmutablePureComponent;
With React 16.6.0
we ware introduced to React.memo
a React.PureComponent
equivalent for functional components. And the same story goes here,
unfortunately React.memo
is not fully embracing Immutable
potential. That
is where immutableMemo
steps in. This is wrapper over React.memo
with
custom comparison function. immutableMemo
accepts component as first argument
and optionally array of property keys or paths the same way as updateOnProps
is working for ImmutablePureComponent
.
export function immutableMemo<Props>(
component: React$ComponentType<Props>,
updateOnProps?: UpdateOn<Props>,
): React$ComponentType<Props>;
In this example component will update when value of me
is change and will
ignore changes of data
, check
or any other property. Component will also
update on change of first element of buzz
or change to type
and will ignore
changes to the rest of the state.
class Example extends ImmutablePureComponent {
state = {
fis: {
buzz: Immutable.List([10, 11])
ignore: 'this',
},
type: undefined,
};
updateOnStates = [
['fis', 'buzz', 0],
'type',
];
updateOnProps = [
['data', 'check', 'me'],
];
render() {...}
}
let data = Immutable.Map({ check: new Map([['me', true]]) })
ReactDOM.render(<Example data={data} onChange={() => {}}, root);
To check what its all about checkout the interactive example :D
FAQs
React PureComponent implementation embracing Immutable.js
The npm package react-immutable-pure-component receives a total of 346,367 weekly downloads. As such, react-immutable-pure-component popularity was classified as popular.
We found that react-immutable-pure-component 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.