Higher order component to memoize derived data
npm install react-memocomponentWillReceiveProps go to 1.
sh
npm install react-memo
`
Examples
These examples demonstrate how you can use this library:
$3
You can pass createWrapper a single property selector.
`js
import React from 'react';
import {createSelector, createWrapper} from 'react-memo';
const TotalViewer = ({total}) => {total};
// The first argument is the alias.
// The second is the array of value selectors.
// These must be pure and very fast.
// Re-evaluation is decided from changes to the returned
// value from these value selectors via ===.
// The last argument is the function that calculates the value
// of the property passed down. Which is called with the
// values returned from selectors in the same order.
// This is where the heavy calculations go.
const selector: any = createSelector('total', [
(props) => props.pocket,
(props) => props.bank,
], (pocket, bank) => / Intense monetary calcuations / pocket + bank);
const TotalViewerWrapper = createWrapper(selector)(TotalViewer);
const TotalMoneyIHave = (props) => ;
`
$3
You can pass createWrapper an array of property selectors and validators as options.
`js
import React from 'react';
import {createSelector, createWrapper} from 'react-memo';
const TotalViewer = ({total, currencySymbol}) =>
{total}{currencySymbol};
const totlaSelector: any = createSelector('total', [
(props) => props.pocket,
(props) => props.bank,
], (pocket, bank, currency) => pocket + bank);
// Value selectors are also passed context.
// However inorder to use context you must provide the contextTypes.
const currencySymbolSelector: any = createSelector('currencySymbol', [
(props, context) => context.currency,
], (currency) => currency ? currency : '$');
const contextTypes = { currency: React.PropTypes.string };
const TotalViewerWrapper = createWrapper([
totlaSelector,
currencySymbolSelector,
], {contextTypes})(TotalViewer);
const TotalMoneyIHave = (props) => ;
`
API Reference
This section describes the functions in detail.
$3
`js
// Returns a property selector you can pass down to createWrapper
function createSelector(
alias: string,
selectors: Array<(props, context) => any>,
resolver: (...values: any[]) => any
): Selector;
// Provide single selector
function createSelector(
alias: string,
selectors: (props, context) => any,
resolver: (value: any) => any
): Selector;
// Provide no selectors, resolver will be called only once
// during the lifetime of the component.
function createSelector(alias: string, resolver: () => any): Selector;
`
- alias: The name of the prop that is to be passed down.
- selectors: The value selector or an array of value selectors that are
used to select the arguments for the resolver. These are also used to judge
whether resolver should be called.
- resolver: The place where the heavy calculations are done.
$3
`js
createWrapper(selectors: Selector, options?: Options): Wrapper;
createWrapper(selectors: Selector[], options?: Options): Wrapper;
// Options: {propTypes, contextTypes};
// Wrapper: (Component) => WrappedComponent;
`
- selectors: A single property selector or an array of them.
- options: The object that may contain propTypes or contextTypes to use in
property validation. Please note that in order to use context in value selectors
you must provide the contextTypes` or React won't pass down the required context.