-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useStateList): implemented
currentIndex
, setState
, `setState…
…At` methods as requested in #634; Reworked a bit implementation of `next` and `prev` to make it reuse the `setStateAt` method;
- Loading branch information
Showing
4 changed files
with
176 additions
and
38 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 |
---|---|---|
@@ -1,25 +1,52 @@ | ||
# `useStateList` | ||
|
||
React state hook that circularly iterates over an array. | ||
Provides handles for circular iteration over states list. | ||
Supports forward and backward iterations and arbitrary position set. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import { useStateList } from 'react-use'; | ||
import { useRef } from 'react'; | ||
|
||
const stateSet = ['first', 'second', 'third', 'fourth', 'fifth']; | ||
|
||
const Demo = () => { | ||
const {state, prev, next} = useStateList(stateSet); | ||
const { state, prev, next, setStateAt, setState, currentIndex } = useStateList(stateSet); | ||
const indexInput = useRef<HTMLInputElement>(null); | ||
const stateInput = useRef<HTMLInputElement>(null); | ||
|
||
return ( | ||
<div> | ||
<pre>{state}</pre> | ||
<pre> | ||
{state} [index: {currentIndex}] | ||
</pre> | ||
<button onClick={() => prev()}>prev</button> | ||
<br /> | ||
<button onClick={() => next()}>next</button> | ||
<br /> | ||
<input type="text" ref={indexInput} style={{ width: 120 }} /> | ||
<button onClick={() => setStateAt((indexInput.current!.value as unknown) as number)}>set state by index</button> | ||
<br /> | ||
<input type="text" ref={stateInput} style={{ width: 120 }} /> | ||
<button onClick={() => setState(stateInput.current!.value)}> set state by value</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
> If the `stateSet` is changed by a shorter one the hook will select the last element of it. | ||
## Reference | ||
|
||
```ts | ||
const { state, currentIndex, prev, next, setStateAt, setState } = useStateList<T>(stateSet: T[] = []); | ||
``` | ||
|
||
If `stateSet` changed, became shorter than before and `currentIndex` left in shrinked gap - the last element of list will be taken as current. | ||
|
||
- **`state`**_`: T`_ — current state value; | ||
- **`currentIndex`**_`: number`_ — current state index; | ||
- **`prev()`**_`: void`_ — switches state to the previous one. If first element selected it will switch to the last one; | ||
- **`nexct()`**_`: void`_ — switches state to the next one. If last element selected it will switch to the first one; | ||
- **`setStateAt(newIndex: number)`**_`: void`_ — set the arbitrary state by index. Indexes are looped, and can be negative. | ||
_4ex:_ if list contains 5 elements, attempt to set index 9 will bring use to the 5th element, in case of negative index it will start counting from the right, so -17 will bring us to the 4th element. | ||
- **`setState(state: T)`**_`: void`_ — set the arbitrary state value that exists in `stateSet`. _In case new state does not exists in `stateSet` an Error will be thrown._ |
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