Giter VIP home page Giter VIP logo

react-memo's Introduction

npm Build Status

Heavily inspired by Reselect.

React Memo is a Higher Order Component wraps the your data derivation logic.

The main objective of this library was(is) to provide memoization logic for Material-UI's inline-styles. But it can also be used for any other class of calculations too.

To use this package you would need to be fairly familiar with React itself.

TL;DR

This is a simply a caching and cache invalidation library that works very well with react.

  1. Take data from prop and contex.
  2. Run value selectors and get needed values.
  3. Run resolver with the values if they are modified from the previous invocation. (resolver must be very cpu/memory intensive for this to have positive effect)
  4. Pass the results down as property.
  5. With each call to componentWillReceiveProps go to 1.

Installation

You can install this package with the following command:

npm install react-memo

Examples

These examples demonstrate how you can use this library:

Simple Usage

You can pass createWrapper a single property selector.

import React from 'react';
import {createSelector, createWrapper} from 'react-memo';

const TotalViewer = ({total}) => <span>{total}</span>;

// 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) => <TotalViewerWrapper pocket={props.pocket} bank={0} />;

Complex Usage

You can pass createWrapper an array of property selectors and validators as options.

import React from 'react';
import {createSelector, createWrapper} from 'react-memo';

const TotalViewer = ({total, currencySymbol}) =>
  <span>{total}{currencySymbol}</span>;

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) => <TotalViewerWrapper pocket={props.pocket} bank={0} />;

API Reference

This section describes the functions in detail.

createSelector

// 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.

createWrapper

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.

Typings

The typescript type definitions are also available and are installed via npm.

License

This project is licensed under the MIT license.

react-memo's People

Contributors

alitaheri avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-memo's Issues

TL;DR

Just to be clear.
I'm right discribing react-memo as a standalone version of reselect (independant of react-redux) ๐Ÿ˜?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.