1.0.1 • Published 6 years ago
use-query-state v1.0.1
useQueryState
The fastest way to use Query String as State
View Demo
Prerequisite
- react ^16.8.0
- react-router-dom ^5.1.0
Install
npm install use-query-stateUsage
just like
setState
Basic
import useQueryState from 'use-query-state';
const App = () => {
const [name, setName] = useQueryState('', 'name');
return (
<div>{ name }</div>
);
}Advanced
import useQueryState from 'use-query-state';
const App = () => {
const [input, setInput] = useQueryState('hello world', 'input', { action: 'replace', delay: 300 });
const onInputChange = (event) => {
setInput(event.target.value);
};
return (
<div>
<input type="text" value={input} onChange={this.handleChange} />
</div>
);
}API
useQueryState(DefaultValue, QueryParamName, Options? ): Value, Setter
DefaultValue
- Type:
string|boolean|number|string[] - Required:
true - Description: the value is used when query parameter is
undefined
QueryParamName
- Type:
string - Required:
true - Description: the valued is used as the query parameter name
Options
- Type:
{ action?: 'push' | 'replace', delay?: number } - Required:
false - Description:
actionis defining how to mutate the history state.actiondefaults to'push'.delayis defining the debounce wait time for mutating the history state when usingSetter.delaydefaults to0.
Value
- Type:
string|boolean|number|string[]|undefined|null - Description: the returned value for read
Setter
- Type:
(value: Value): void - Description: The setter used for setting the state
License
MIT