-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
5,187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"bracketSpacing": true, | ||
"semi": true, | ||
"useTabs": false, | ||
"jsxBracketSameLine": false, | ||
"printWidth": 80, | ||
"trailingComma": "all", | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2018 Pier Paolo Ramon | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Hooks’ Macro :umbrella: | ||
|
||
> Babel Macros for React Hooks automatic memoization invalidation. | ||
## Installation | ||
|
||
Requires [`babel-plugin-macros`](https://www.npmjs.com/package/babel-plugin-macros), which is already configured for you if you are using Create React App v2+. | ||
|
||
``` | ||
npm install --dev hooks.macro | ||
``` | ||
|
||
``` | ||
yarn add --dev hooks.macro | ||
``` | ||
|
||
## Usage | ||
|
||
Replace: | ||
|
||
```js | ||
import { useMemo } from "react"; | ||
|
||
function MyComponent({ labels }) { | ||
const myComputation = useMemo( | ||
() => labels.map(label => label.toUpperCase()), | ||
[labels] | ||
); | ||
} | ||
``` | ||
|
||
With: | ||
|
||
```js | ||
import { useAutoMemo } from "hooks.macro"; | ||
|
||
function MyComponent({ labels }) { | ||
const myComputation = useAutoMemo(() => | ||
labels.map(label => label.toUpperCase()) | ||
); | ||
} | ||
``` | ||
|
||
Or even: | ||
|
||
```js | ||
import { useAutoMemo } from "hooks.macro"; | ||
|
||
function MyComponent({ labels }) { | ||
const myComputation = useAutoMemo(labels.map(label => label.toUpperCase())); | ||
} | ||
``` | ||
|
||
## Full reference | ||
|
||
### `useAutoMemo` | ||
|
||
Exactly like React’s `useMemo` but automatically identifies value dependencies. | ||
|
||
Can be passed a factory function or directly a value, will convert the latter to a function for you. | ||
|
||
```js | ||
import { useAutoMemo } from "hooks.macro"; | ||
``` | ||
|
||
```js | ||
useAutoMemo(value); | ||
useAutoMemo(() => value); | ||
``` | ||
|
||
Both become: | ||
|
||
```js | ||
useMemo(() => value, [value]); | ||
``` | ||
|
||
### `useAutoCallback` | ||
|
||
Exactly like React’s `useMemo` but automatically identifies value dependencies. | ||
|
||
```js | ||
import { useAutoCallback } from "hooks.macro"; | ||
``` | ||
|
||
```js | ||
useAutoCallback(() => { | ||
doSomethingWith(value); | ||
}); | ||
``` | ||
|
||
Becomes: | ||
|
||
```js | ||
useCallback( | ||
() => { | ||
doSomethingWith(value); | ||
}, | ||
[doSomethingWith, value] | ||
); | ||
``` | ||
|
||
## Limitations | ||
|
||
To make this work I currently needed to pose some limitations. This could change in the future (PR ___very___ welcome). | ||
|
||
1. Only variables created in the scope of the component body are automatically trapped as value dependencies. | ||
|
||
2. Only variables, and not properties’ access, are trapped. This means that if you use `obj.prop` only `[obj]` will become part of the memoization invalidation keys. This is a problem for refs, and will be addressed specifically in a future release. | ||
|
||
3. Currently there’s no way to add additional keys for more fine grained cache invalidation. Could be an important escape hatch when you do nasty things, but in that case I’d prefer to use `useMemo`/`useCallback` directly. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: ['@babel/preset-env'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export function useAutoMemo<T>(factory: () => T): T; | ||
export function useAutoMemo<T>(factory: T): T; | ||
|
||
export function useCallback<T extends (...args: any[]) => any>(callback: T): T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "hooks.macro", | ||
"version": "0.1.0", | ||
"author": "Pier Paolo Ramon <ramonpierre@gmail.com>", | ||
"main": "build/hooks.macro.js", | ||
"license": "MIT", | ||
"repository": "https://github.com/yuchi/hooks.macro.git", | ||
"typings": "./index.d.ts", | ||
"scripts": { | ||
"test": "jest", | ||
"test:update": "jest --updateSnapshot", | ||
"build": "babel src/hooks.macro.js -d build", | ||
"format": "prettier --write \"**/*.ts\" \"**/*.json\" \"**/*.js\"" | ||
}, | ||
"dependencies": { | ||
"@babel/helper-module-imports": "^7.0.0", | ||
"babel-plugin-macros": "^2.4.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.5", | ||
"@babel/core": "^7.1.6", | ||
"@babel/preset-env": "^7.1.6", | ||
"babel-core": "7.0.0-bridge.0", | ||
"babel-jest": "^23.6.0", | ||
"babel-plugin-macros": "^2.4.2", | ||
"babel-plugin-tester": "^5.5.2", | ||
"jest": "^23.6.0", | ||
"prettier": "^1.15.2" | ||
} | ||
} |
Oops, something went wrong.