react-query-to-state
Mapping query string from the path to Component state seamlessly.
Installation
Use npm
npm install react-query-to-state
Use yarn
yarn add react-query-to-state
Usage
ES6 Class Component
Component { const updateQueryState = thisprops } { const query = thisprops const searchStr = query return <div> <span>searchStr</span> <input onChange=thishandleChange /> </div> } const config = history: // optional initState: searchStr: 'abcde' validatorMap: searchStr: type: 'regexp' value: /^abc/i isReplace: true Searcher config
Functional Component
const Searcher = { const handleChange = { const updateQueryState = props } const query = props const searchStr = query return <div> <span>searchStr</span> <input onChange=handleChange /> </div> } const config = initState: searchStr: '' Searcher config
Multiple Components in one page
Component { const searchStr1 = thisprops return <div>searchStr1</div> } const FunctionalSearcher = { const searchStr2 = props return <div>searchStr2</div>} const config1 = initState: searchStr1: 'str1' validatorMap: searchStr1: type: 'range' value: 'aaa' 'bbb' const config2 = initState: searchStr2: 'str2' validatorMap: searchStr2: type: 'function' { return val } const SearcherQueryToStateComp = const FunctionalSearcherQueryToStateComp = Component { return <ReactFragment> <SearcherQueryToStateComp /> <FunctionalSearcherQueryToStateComp /> </ReactFragment> }
Configuration
Name | Type | Default | Description |
---|---|---|---|
history |
object |
createBrowserHistory() | Optional . History object, see history for more information. |
isReplace |
boolean |
true |
Optional . If true , history.replace() will be called, or history.push() will be called when queryState is updated by component. |
initState |
object |
Only properties declared in the initState object will be mapped from the path to Component state. | |
validatorMap |
object |
Optional . ValidatorMap is a dictionary of properties validators. The key is a property name, and the value is an array of validator for this property. |
ValidatorMap
ValidatorMap is a dictionary of properties validators. The key is a property, and the value is an array of validator for this property.
validateTypes
:
- range
- regexp
- function
const validatorMap = key1: type: 'range' value: 'aa' 'bb' 'cc' key2: type: 'regexp' value: /^test/i type: 'function' { return val }