hooks.macro
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -7,4 +7,3 @@ "use strict"; | ||
var _require2 = require('babel-plugin-macros'), | ||
createMacro = _require2.createMacro, | ||
MacroError = _require2.MacroError; | ||
createMacro = _require2.createMacro; | ||
@@ -60,7 +59,7 @@ module.exports = createMacro(memoMacro); | ||
} else { | ||
throw new MacroError("".concat(macroName, " must be called with a function or an arrow")); | ||
throw state.file.buildCodeFrameError(argument && argument.node || path.node, "".concat(macroName, " must be called with a function or an arrow")); | ||
} | ||
} | ||
var CONFIGS = [['useAutoMemo', 'useMemo', true], ['useAutoCallback', 'useCallback', false]]; | ||
var CONFIGS = [['useAutoMemo', 'useMemo', true], ['useAutoCallback', 'useCallback', false], ['useAutoEffect', 'useEffect', false], ['useAutoMutationEffect', 'useMutationEffect', false], ['useAutoLayoutEffect', 'useLayoutEffect', false]]; | ||
@@ -82,3 +81,3 @@ function memoMacro(_ref) { | ||
} else { | ||
throw new MacroError("".concat(macroName, " can only be used a function, and can not be passed around as an argument.")); | ||
throw state.file.buildCodeFrameError(referencePath.node, "".concat(macroName, " can only be used a function, and can not be passed around as an argument.")); | ||
} | ||
@@ -85,0 +84,0 @@ }); |
{ | ||
"name": "hooks.macro", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"author": "Pier Paolo Ramon <ramonpierre@gmail.com>", | ||
@@ -5,0 +5,0 @@ "main": "build/hooks.macro.js", |
@@ -22,3 +22,3 @@ # Hooks’ Macro :umbrella: | ||
```js | ||
import { useMemo } from "react"; | ||
import { useMemo } from 'react'; | ||
@@ -28,3 +28,3 @@ function MyComponent({ labels }) { | ||
() => labels.map(label => label.toUpperCase()), | ||
[labels] | ||
[labels], | ||
); | ||
@@ -37,7 +37,7 @@ } | ||
```js | ||
import { useAutoMemo } from "hooks.macro"; | ||
import { useAutoMemo } from 'hooks.macro'; | ||
function MyComponent({ labels }) { | ||
const myComputation = useAutoMemo(() => | ||
labels.map(label => label.toUpperCase()) | ||
labels.map(label => label.toUpperCase()), | ||
); | ||
@@ -50,3 +50,3 @@ } | ||
```js | ||
import { useAutoMemo } from "hooks.macro"; | ||
import { useAutoMemo } from 'hooks.macro'; | ||
@@ -67,3 +67,3 @@ function MyComponent({ labels }) { | ||
```js | ||
import { useAutoMemo } from "hooks.macro"; | ||
import { useAutoMemo } from 'hooks.macro'; | ||
``` | ||
@@ -87,3 +87,3 @@ | ||
```js | ||
import { useAutoCallback } from "hooks.macro"; | ||
import { useAutoCallback } from 'hooks.macro'; | ||
``` | ||
@@ -104,9 +104,38 @@ | ||
}, | ||
[doSomethingWith, value] | ||
[doSomethingWith, value], | ||
); | ||
``` | ||
### `useAutoEffect`, `useAutoMutationEffect`, `useAutoLayoutEffect` | ||
They work exactly like their standard React counterpart, but they automatically identify value dependencies. | ||
```js | ||
import { | ||
useAutoEffect, | ||
useAutoMutationEffect, | ||
useAutoLayoutEffect, | ||
} from 'hooks.macro'; | ||
``` | ||
```js | ||
useAutoEffect(() => { | ||
doSomethingWith(value); | ||
}); | ||
``` | ||
Becomes: | ||
```js | ||
useEffect( | ||
() => { | ||
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). | ||
To make this work I currently needed to pose some limitations. This could change in the future (PR **_very_** welcome). | ||
@@ -117,2 +146,4 @@ 1. Only variables created in the scope of the component body are automatically trapped as value dependencies. | ||
You can work around this limitation by creating a variable which holds the current value, such as `const { current } = ref`. | ||
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. | ||
@@ -122,2 +153,2 @@ | ||
MIT | ||
MIT |
const { addNamed } = require('@babel/helper-module-imports'); | ||
const { createMacro, MacroError } = require('babel-plugin-macros'); | ||
const { createMacro } = require('babel-plugin-macros'); | ||
@@ -69,3 +69,4 @@ module.exports = createMacro(memoMacro); | ||
} else { | ||
throw new MacroError( | ||
throw state.file.buildCodeFrameError( | ||
(argument && argument.node) || path.node, | ||
`${macroName} must be called with a function or an arrow`, | ||
@@ -79,2 +80,5 @@ ); | ||
['useAutoCallback', 'useCallback', false], | ||
['useAutoEffect', 'useEffect', false], | ||
['useAutoMutationEffect', 'useMutationEffect', false], | ||
['useAutoLayoutEffect', 'useLayoutEffect', false], | ||
]; | ||
@@ -101,3 +105,4 @@ | ||
} else { | ||
throw new MacroError( | ||
throw state.file.buildCodeFrameError( | ||
referencePath.node, | ||
`${macroName} can only be used a function, and can not be passed around as an argument.`, | ||
@@ -104,0 +109,0 @@ ); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24704
358
146