Set of a helpful hooks, for different specific to some primitives types state changing helpers
npm install gm-react-hanger
Set of a helpful hooks, for different specific to some primitives types state changing helpers.
Has two APIs:
- First and original from v1 is based on object destructuring e.g. const { value, toggle } = useBoolean(false) (Docs below)
- Second API (recommended why?) is based on more idiomatic to React hooks API, e.g. like useState with array destructuring
const [value, actions] = useBoolean(false) (Docs)
``bash`
yarn add gm-react-hanger
`jsx
import React, { Component } from 'react';
import { useInput, useBoolean, useNumber, useArray, useOnMount, useOnUnmount } from 'react-hanger';
const App = () => {
const newTodo = useInput('');
const showCounter = useBoolean(true);
const limitedNumber = useNumber(3, { lowerLimit: 0, upperLimit: 5 });
const counter = useNumber(0);
const todos = useArray(['hi there', 'sup', 'world']);
const rotatingNumber = useNumber(0, {
lowerLimit: 0,
upperLimit: 4,
loop: true,
});
return (
$3
API reference (object destructuring)
$3
`
import { useBoolean } from 'react-hanger' // will import all of functions
import useBoolean from 'react-hanger/useBoolean' // will import only this function
`$3
Just an alternative syntax to
useState, because it doesn't need array destructuring.
It returns an object with value and a setValue method.`jsx
const username = useStateful('test');username.setValue('tom');
console.log(username.value);
`$3
`jsx
const showCounter = useBoolean(true);
`Methods:
-
toggle
- setTrue
- setFalse$3
`jsx
const counter = useNumber(0);
const limitedNumber = useNumber(3, { upperLimit: 5, lowerLimit: 3 });
const rotatingNumber = useNumber(0, {
upperLimit: 5,
lowerLimit: 0,
loop: true,
});
`Methods:
Both
increase and decrease take an optional amount argument which is 1 by default, and will override the step property if it's used in the options.-
increase(amount = 1)
- decrease(amount = 1 )Options:
-
lowerLimit
- upperLimit
- loop
- step - sets the increase/decrease amount of the number upfront, but it can still be overriden by number.increase(3) or number.decrease(5)$3
`jsx
const newTodo = useInput('');
``jsx
``jsx
`Methods:
-
clear
- onChange
- eventBind - binds the value and onChange props to an input that has e.target.value
- valueBind - binds the value and onChange props to an input that's using only value in onChange (like most external components)Properties:
-
hasValue$3
`jsx
const todos = useArray([]);
`Methods:
-
add
- clear
- removeIndex
- removeById - if array consists of objects with some specific id that you pass
all of them will be removed
- move - moves item from position to position shifting other elements.`
So if input is [1, 2, 3, 4, 5] from | to | expected
3 | 0 | [4, 1, 2, 3, 5]
-1 | 0 | [5, 1, 2, 3, 4]
1 | -2 | [1, 3, 4, 2, 5]
-3 | -4 | [1, 3, 2, 4, 5]
`$3
`jsx
const { value, set } = useMap([['key', 'value']]);
const { value: anotherValue, remove } = useMap(new Map([['key', 'value']]));
`Actions:
-
set
- remove
- clear
- initialize - applies tuples or map instances
- setValue$3
`jsx
const { state, setState } = useSetState({ loading: false });
setState({ loading: true, data: [1, 2, 3] });
`Methods:
-
setState(value) - will merge the value with the current state (like this.setState works in React)Properties:
-
state - the current state$3
Use it to get the previous value of a prop or a state value.
It's from the official React Docs.
It might come out of the box in the future.
`jsx
const Counter = () => {
const [count, setCount] = useState(0);
const prevCount = usePrevious(count);
return (
Now: {count}, before: {prevCount}
);
};
`$3
return bound dispatch actions
`ts
const { action1, action2 } = useActions(actions);
// dispatch action
action1();
`$3
see lodash debounce
`ts
const fetch = () {...};
const debounceFetch = useDebounce(fetch);
`$3
$3
`ts
/**
* save the current application state to top.history.state
* restore the status of the application when refreshing or rebounding
*/
const App = () => {
/**
* react-router history, In general: stateContainer._history
*/
useRestore(history);
...
}`$3
see Making setInterval Declarative with React Hooks
$3
`ts
const { rt, start } = useCountdown(SECOND); const [text, setText] = useState('发送验证码');
useEffect(() => {
if (rt > 0) {
setText(
${rt}秒后重试);
} else {
setText('发送验证码');
}
}, [rt]); // button click -> start
``