recoil-nexus
Advanced tools
Comparing version 0.5.0 to 0.5.1
{ | ||
"name": "recoil-nexus", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "A small Typescript package to access your Recoil atoms outside of React components.", | ||
@@ -5,0 +5,0 @@ "main": "build/RecoilNexus.js", |
@@ -51,36 +51,25 @@ # R E C O I L - N E X U S | ||
| `getRecoilPromise` | getter function, returns a promise. To be used with asynchronous selectors. | | ||
| `setRecoil` | setter function, pass value to be set as second parameter | | ||
| `setRecoil` | setter function, pass value to be set or updater function as second parameter | | ||
| `resetRecoil` | pass atom as parameter to reset to default value | | ||
Read current state: | ||
```tsx | ||
// Loading example | ||
import { loadingState } from "../atoms/loadingState"; | ||
import { getRecoil, setRecoil } from "recoil-nexus"; | ||
export default function toggleLoading() { | ||
const loading = getRecoil(loadingState); | ||
setRecoil(loadingState, !loading); | ||
} | ||
const loading = getRecoil(loadingState); | ||
``` | ||
Setting the new state like this is not inherently wrong: | ||
```tsx | ||
//Loader | ||
import React from "react"; | ||
import { useRecoilValue } from "recoil"; | ||
setRecoil(loadingState, !loading); | ||
``` | ||
However, if the new state depends on the previous one (like in this case), preferably use an | ||
updater function to correctly batch React state updates, as reading and | ||
updating the state at the same time could lead to [unexpected results](https://github.com/luisanton-io/recoil-nexus/issues/33). | ||
```tsx | ||
import { setRecoil } from "recoil-nexus" | ||
export default function Loader() { | ||
loading = useRecoilValue(loadingState); | ||
return loading ? <h3>Loading...</h3> : null; | ||
export function toggleLoader() { | ||
setRecoil(loadingState, loading => !loading) | ||
} | ||
``` | ||
```tsx | ||
//Atom | ||
import { atom } from "recoil"; | ||
export const loadingState = atom({ | ||
key: "LOADING", | ||
default: false, | ||
}); | ||
``` | ||
## Test Setup | ||
@@ -87,0 +76,0 @@ |
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
10989
100