npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-derive

v0.1.1

Published

Derived data for your memoizing pleasure

Downloads

5

Readme

react-derive

For organizing and optimizing rendering of react components that rely on derived data. Works by wrapping your component in a HoC. For example, lets say your component shows the result of adding two numbers.

export default class Add extends Component {
  render() {
    const {a,b,fontSize} = this.props;
    return <div style={{fontSize}}>a + b = {a+b}</div>
  }
}

We can move the calculation of a+b to a decorator named @derive where we'll create the deriver function named sum. And because we named the function sum, the deriver's result will be passed into the Add component via a prop likewise named sum.

@derive({
  sum({a,b}) { return a+b }
})
export default class Add extends Component {
  render() {
    const {sum,fontSize} = this.props;
    return <div style={{fontSize}}>a + b = {sum}</div>
  }
}

Note that

  • The first argument to a deriver function is newProps.
  • The second argument is the previously derived props object (in this case it would look something like {a:5,b:3,sum:8})
  • The value of this allows you to reference the result of other derivers like this.sum().

But wait, every time the component renders, sum will recalculate even if a and b didn't change. To optimize, we can memoize the calculation with @track so when the fontSize prop changes sum won't be recalculated.

@derive({
  @track('a', 'b')
  sum({a,b}) { return a+b }
})
export default class Add extends Component {
  render() {
    const {sum,fontSize} = this.props;
    return <div style={{fontSize}}>a + b = {sum}</div>
  }
}

We supply args 'a' and 'b' to the @track decorator to indicate that the sum deriver only cares about those two props. If fontSize changes, sum won't recalculate.


This project is similar to reselect for redux. reselect has a very nice way to compose selector functions. However, react-derive offers a different approach

@derive as a decorator

You can use this object to depend on other derived props:

@derive({
  @track('taxPercent')
  tax({taxPercent}) {
    return this.subtotal() * (taxPercent / 100);
  },

  @track('items')
  subtotal({items}) {
    return items.reduce((acc, item) => acc + item.value, 0);
  },

  @track('taxPercent')
  total({taxPercent}) {
    return this.subtotal() + this.tax();
  }
})
class Total extends React.Component {
  render() {
    return <div>{ this.props.total }</div>
  }
}

See the reselect version of the example above

Derive as a Component

options prop is the same as first argument to @derive. The child is a function that accepts the derived props object as it's first argument:

<Derive {...{taxPercent, items}} options={deriveOptions}>
{({tax, subtotal, total}) =>
  <ul>
    <li>tax: {tax}</li>
    <li>subtotal: {subtotal}</li>
    <li>total: {total}</li>
  </ul>
}</Derive>

ES6 support

Using ES7 decorators is in fact optional. If you want to stick with ES6 constructs, it's easy to do:

export const Add =
  (derive({
    sum: track('a','b')
      (function({a,b}) { return a+b })
  })
  class Add extends Component {
    render() {
      const {sum,fontSize} = this.props;
      return <div style={{fontSize}}>a + b = {sum}</div>
    }
  });
  

See the examples/ dir of this repo for additional examples.

install + import

npm i react-derive -S

then:

import {Derive, derive, track} from 'react-derive';

or when included via script tag it's available as the global variable ReactDerive:

const {Derive, derive, track} = ReactDerive;

documentation

examples