-
-
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.
- Loading branch information
Showing
24 changed files
with
2,514 additions
and
1,780 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
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
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 |
---|---|---|
@@ -1,4 +1,35 @@ | ||
--- | ||
id: cleanup | ||
title: cleanup | ||
title: cleanup() | ||
--- | ||
|
||
function that dispatches an action to delete the state from redux store. | ||
useful when you dont want to persist the state when component unmounted. | ||
|
||
*function is provided by `use-redux-state-hook`* | ||
|
||
```js | ||
cleanup() | ||
``` | ||
|
||
## `Example` | ||
|
||
```jsx | ||
import { useEffect } from 'react' | ||
import useReduxState from 'use-redux-state-hook' | ||
const Component = () => { | ||
|
||
const {cleanup} = useReduxState({ | ||
state: { | ||
state1: 'a', | ||
state2: 'b' | ||
}, | ||
name: 'component_state' | ||
}) | ||
|
||
useEffect(() => { | ||
return () => cleanup() | ||
/* component_state: undefined */ | ||
}, []) | ||
} | ||
``` |
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 |
---|---|---|
@@ -1,4 +1,44 @@ | ||
--- | ||
id: get-state | ||
title: getState | ||
title: getState() | ||
--- | ||
|
||
function to get the state for a given state name. | ||
|
||
## `Arguments` | ||
|
||
### **stateName** | ||
|
||
name of the nest-able state to be selected | ||
|
||
```js | ||
getState(store, stateName, getter?: (state) => selectedState) | ||
``` | ||
|
||
## `Returns` | ||
|
||
### **stateValue** | ||
|
||
```js | ||
getState(store, 'todos.completed', (state) => state.todos) // any value | ||
``` | ||
|
||
## `Example` | ||
|
||
```jsx | ||
import { useEffect } from 'react' | ||
import useReduxState, { getState } from 'use-redux-state-hook' | ||
const Component = () => { | ||
useReduxState({ | ||
state: { | ||
state1: [], | ||
}, | ||
name: 'todos.completed' | ||
}) | ||
|
||
useEffect(() => { | ||
console.log(getState(store, 'todos.completed', (state) => state.todos)) | ||
// {state1: []} | ||
}, []) | ||
} | ||
``` |
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,42 @@ | ||
--- | ||
id: use-get-state | ||
title: useGetState() | ||
--- | ||
|
||
This hook gives you a getState function for a given state | ||
|
||
## Arguments | ||
|
||
### `stateName` | ||
|
||
name of the nestable state to be selected | ||
|
||
## Returns | ||
|
||
### `getState()` | ||
|
||
```js | ||
getState(stateName) | ||
``` | ||
|
||
## Example | ||
|
||
```jsx | ||
import { useEffect } from 'react' | ||
import useReduxState, { useGetState } from 'use-redux-state-hook' | ||
const Component = () => { | ||
useReduxState({ | ||
state: { | ||
state1: 'a', | ||
state2: 'b' | ||
}, | ||
name: 'component_state' | ||
}) | ||
const getState = useGetState('component_state') | ||
|
||
useEffect(() => { | ||
console.log(getState((component_state) => component_state?.state2)) | ||
// 'c' | ||
}, []) | ||
} | ||
``` |
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 |
---|---|---|
@@ -1,45 +1,59 @@ | ||
--- | ||
id: use-memo-selector | ||
title: useMemoSelector | ||
title: useMemoSelector() | ||
--- | ||
|
||
This hook allows to select redux state efficiently and in a memoized way. | ||
it uses [createSelector](https://github.com/reduxjs/reselect#createselectorinputselectors--inputselectors-resultfunc) to make sure selector is not recomputed unless one of its arguments changes. | ||
it also uses [react-fast-compare](https://gg.com) to prevent unnecessary updates. | ||
it also uses [react-fast-compare](https://github.com/FormidableLabs/react-fast-compare) to prevent unnecessary updates. | ||
|
||
## Arguments | ||
### selector()||stateName'' | ||
#### selector() | ||
|
||
### `selector()||stateName''` | ||
|
||
#### `selector()` | ||
|
||
function that selects from the state. | ||
receives 1 argument | ||
|
||
``` | ||
(storeState) => selectedState | ||
``` | ||
#### stateName | ||
|
||
#### `stateName` | ||
|
||
name of the nestable state to be selected | ||
|
||
### result() | ||
### `result()` | ||
|
||
function that returns the result of the selected state | ||
|
||
``` | ||
(selectedState) => selectedState | ||
``` | ||
### equality() | ||
function that compare prevState against newState to determine if selected state has changed. defaults to [react-fast-compare](https://gg.com) | ||
|
||
### `equality()` | ||
|
||
function that compare prevState against newState to determine if selected state has changed. defaults to [react-fast-compare](https://github.com/FormidableLabs/react-fast-compare) | ||
|
||
## Example | ||
|
||
```jsx | ||
import useReduxState, {useMemoSelector} from 'use-redux-state-hook' | ||
import useReduxState, { useMemoSelector } from 'use-redux-state-hook' | ||
const Component = () => { | ||
const {selector} = useReduxState({ | ||
const { selector } = useReduxState({ | ||
state: { | ||
state1: 'a', | ||
state2: 'b', | ||
state2: 'b' | ||
}, | ||
name: 'component_state' | ||
}) | ||
const state1 = useMemoSelector(selector, (component_state) => component_state?.state1) | ||
const state1 = useMemoSelector( | ||
selector, | ||
(component_state) => component_state?.state1 | ||
) | ||
const state2 = useMemoSelector('component_state.state2') | ||
|
||
console.log({state1, state2}) // {state1: 'a', state2: 'b'} | ||
console.log({ state1, state2 }) // {state1: 'a', state2: 'b'} | ||
} | ||
``` |
Oops, something went wrong.