{"id":17133273,"url":"https://github.com/gilbox/react-derive","last_synced_at":"2025-04-13T08:12:02.941Z","repository":{"id":34908371,"uuid":"38950890","full_name":"gilbox/react-derive","owner":"gilbox","description":"derived data for your memoizing pleasure","archived":false,"fork":false,"pushed_at":"2015-10-10T18:45:04.000Z","size":836,"stargazers_count":16,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T08:11:53.751Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://gilbox.github.io/react-derive/index.js.html","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gilbox.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-12T05:16:28.000Z","updated_at":"2019-07-08T18:21:19.000Z","dependencies_parsed_at":"2022-09-01T06:02:09.692Z","dependency_job_id":null,"html_url":"https://github.com/gilbox/react-derive","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbox%2Freact-derive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbox%2Freact-derive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbox%2Freact-derive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbox%2Freact-derive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gilbox","download_url":"https://codeload.github.com/gilbox/react-derive/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681497,"owners_count":21144700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T19:41:48.243Z","updated_at":"2025-04-13T08:12:02.913Z","avatar_url":"https://github.com/gilbox.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-derive\n\nFor **organizing** and **optimizing** rendering of react components\nthat rely on derived data. Works by wrapping your component in a HoC.\nFor example, lets say your\ncomponent shows the result of adding two numbers.\n\n    export default class Add extends Component {\n      render() {\n        const {a,b,fontSize} = this.props;\n        return \u003cdiv style={{fontSize}}\u003ea + b = {a+b}\u003c/div\u003e\n      }\n    }\n\nWe can move the calculation of `a+b` to a decorator named `@derive`\nwhere we'll create the *deriver* function named `sum`. And because we\nnamed the function `sum`, the deriver's result will be passed\ninto the `Add` component via a prop likewise named `sum`.\n\n    @derive({\n      sum({a,b}) { return a+b }\n    })\n    export default class Add extends Component {\n      render() {\n        const {sum,fontSize} = this.props;\n        return \u003cdiv style={{fontSize}}\u003ea + b = {sum}\u003c/div\u003e\n      }\n    }\n\nNote that\n\n- The first argument to a *deriver* function is `newProps`.\n- The second argument is the previously derived props object\n  (in this case it would look something like `{a:5,b:3,sum:8}`)\n- The value of `this` allows you to reference the result of other\n  derivers like `this.sum()`.\n\nBut wait, every time the component renders, `sum` will recalculate even\nif `a` and `b` didn't change. To optimize, we can memoize the calculation with `@track`\nso when the `fontSize` prop changes `sum` won't be recalculated.\n\n    @derive({\n      @track('a', 'b')\n      sum({a,b}) { return a+b }\n    })\n    export default class Add extends Component {\n      render() {\n        const {sum,fontSize} = this.props;\n        return \u003cdiv style={{fontSize}}\u003ea + b = {sum}\u003c/div\u003e\n      }\n    }\n\nWe supply args `'a'` and `'b'` to the `@track`\ndecorator to indicate that the `sum` deriver only\ncares about those two props. If `fontSize` changes,\n`sum` won't recalculate.\n\n-------\n\nThis project is similar to [reselect](https://github.com/faassen/reselect)\nfor redux. However, while reselect helps manage derived data from \nglobal state, react-derive manages derived data from props.\n\n## `@derive` as a decorator\n\nYou can use `this` object to depend on other derived props:\n\n    @derive({\n      @track('taxPercent')\n      tax({taxPercent}) {\n        return this.subtotal() * (taxPercent / 100);\n      },\n\n      @track('items')\n      subtotal({items}) {\n        return items.reduce((acc, item) =\u003e acc + item.value, 0);\n      },\n\n      @track('taxPercent')\n      total({taxPercent}) {\n        return this.subtotal() + this.tax();\n      }\n    })\n    class Total extends React.Component {\n      render() {\n        return \u003cdiv\u003e{ this.props.total }\u003c/div\u003e\n      }\n    }\n\nSee the [reselect version of the example above](https://github.com/faassen/reselect#example)\n\n## `Derive` as a Component\n\n`options` prop is the same as first argument to `@derive`.\nThe child is a function that accepts the derived props object\nas it's first argument:\n\n    \u003cDerive {...{taxPercent, items}} options={deriveOptions}\u003e\n    {({tax, subtotal, total}) =\u003e\n      \u003cul\u003e\n        \u003cli\u003etax: {tax}\u003c/li\u003e\n        \u003cli\u003esubtotal: {subtotal}\u003c/li\u003e\n        \u003cli\u003etotal: {total}\u003c/li\u003e\n      \u003c/ul\u003e\n    }\u003c/Derive\u003e\n\n## ES6 support\n\nUsing ES7 decorators is in fact optional. If you want to stick with\nES6 constructs, it's easy to do:\n\n    export const Add =\n      derive({\n        sum: track('a','b')\n          (function({a,b}) { return a+b })\n      })                                   // \u003c--- function returned...\n      (class Add extends Component {       // \u003c--- immediately invoked by passing in class\n        render() {\n          const {sum,fontSize} = this.props;\n          return \u003cdiv style={{fontSize}}\u003ea + b = {sum}\u003c/div\u003e\n        }\n      });\n      \nSee the `examples/` dir of this repo for additional examples.\n\n## install + import\n\n    npm i react-derive -S\n\nthen:\n\n    import {Derive, derive, track} from 'react-derive';\n\nor when included via script tag it's available as the global variable `ReactDerive`:\n\n    const {Derive, derive, track} = ReactDerive;\n\n## [documentation](http://gilbox.github.io/react-derive/index.js.html)\n\n## examples\n\n- decorator demo:  [source](https://github.com/gilbox/react-derive/blob/master/examples/decorator-demo/app.js) - [live demo](http://gilbox.github.io/react-derive/examples/decorator-demo/demo.html)\n- component demo:  [source](https://github.com/gilbox/react-derive/blob/master/examples/component-demo/app.js) - [live demo](http://gilbox.github.io/react-derive/examples/component-demo/demo.html)\n- [elegant-react-hot-demo](https://github.com/gilbox/elegant-react-hot-demo) (still a WIP)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilbox%2Freact-derive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgilbox%2Freact-derive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilbox%2Freact-derive/lists"}