generated from iamogbz/node-js-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: call enhancer with observable context value (#36)
* fix: call enhancer with observable context value * fix: enhance once before passing value to context * fix: ensure enhanced value is preserved * test: test useAccessor hook * refactor: clearer useAccessor initial prop use
- Loading branch information
Showing
10 changed files
with
291 additions
and
181 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,86 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es6: true, | ||
jest: true, | ||
node: true, | ||
}, | ||
extends: [ | ||
"plugin:@typescript-eslint/recommended", | ||
"plugin:jest/all", | ||
"plugin:prettier/recommended", | ||
"plugin:react/recommended", | ||
"plugin:react-hooks/recommended", | ||
"prettier/@typescript-eslint", | ||
], | ||
globals: { | ||
browser: false, | ||
}, | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
ecmaVersion: 6, | ||
sourceType: "module", | ||
}, | ||
rules: { | ||
"@typescript-eslint/no-unused-vars": [ | ||
1, | ||
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_" }, | ||
], | ||
"arrow-body-style": [2, "as-needed"], | ||
"class-methods-use-this": 0, | ||
"jest/no-hooks": 0, | ||
"jest/prefer-expect-assertions": 0, | ||
"no-console": 1, | ||
"no-param-reassign": ["error", { props: false }], | ||
"no-trailing-spaces": 2, | ||
"no-unused-vars": "off", | ||
"no-use-before-define": "off", | ||
"object-curly-newline": [ | ||
2, | ||
{ | ||
consistent: true, | ||
multiline: true, | ||
}, | ||
], | ||
"object-property-newline": [ | ||
2, | ||
{ | ||
allowAllPropertiesOnSameLine: true, | ||
}, | ||
], | ||
"prettier/prettier": [ | ||
2, | ||
{ | ||
bracketSpacing: true, | ||
jsxBracketSameLine: false, | ||
semi: true, | ||
singleQuote: false, | ||
tabWidth: 4, | ||
trailingComma: "all", | ||
}, | ||
], | ||
"react/jsx-first-prop-new-line": [2, "multiline"], | ||
"react/jsx-indent": [2, 4], | ||
"react/jsx-indent-props": [2, 4], | ||
"sort-imports": [ | ||
2, | ||
{ | ||
ignoreDeclarationSort: true, | ||
}, | ||
], | ||
"sort-keys": "warn", | ||
}, | ||
settings: { | ||
"import/resolver": { | ||
node: { | ||
paths: ["src"], | ||
}, | ||
}, | ||
react: { | ||
version: "detect", | ||
}, | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,21 @@ | ||
import * as React from "react"; | ||
|
||
export function useAccessor<R>( | ||
value?: R, | ||
): [() => R | undefined, ((v: R) => void) | undefined] { | ||
const ref = React.useRef(value); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const initialized = React.useMemo(() => value !== undefined, []); | ||
|
||
const getValue = React.useCallback(function getValue() { | ||
return ref.current; | ||
}, []); | ||
const setValue = React.useCallback(function setValue(value) { | ||
ref.current = value; | ||
}, []); | ||
|
||
// If a value was initially provided then keep the ref updated | ||
if (initialized) setValue(value); | ||
// If a value was initially provided then no setter is returned | ||
return [getValue, initialized ? undefined : setValue]; | ||
} |
This file was deleted.
Oops, something went wrong.
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
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,8 @@ | ||
import * as React from "react"; | ||
|
||
export function useOnce(callback: React.EffectCallback): void { | ||
const isFirstRun = React.useRef(true); | ||
if (!isFirstRun.current) return; | ||
callback(); | ||
isFirstRun.current = false; | ||
} |
Oops, something went wrong.