react-query-string-to-props
Mapping query string from url to Component props seamlessly. react-query-string-to-props
Installation
Use npm
npm install react-query-string-to-props
Use yarn
yarn add react-query-string-to-props
Usage
ES6 Class Component
Component { const updateQueryState = thisprops } { const searchStr = thisprops return <div> <span>searchStr</span> <input onChange=thishandleChange /> </div> } const config = history: queryPropsConfig: searchStr: QueryPropTypesstring defaultQueryProps: searchStr: 'abcde' validatorMap: searchStr: type: ValidateTypesregexp value: /^abc/i replaceRouteWhenChange: true mapDefaultQueryPropsToUrlWhenMounted: true Searcher config
Functional Component
const Searcher = { const handleChange = { const updateQueryState = props } const searchStr = props return <div> <span>searchStr</span> <input onChange=handleChange /> </div> } const config = history: queryPropsConfig: searchStr: QueryPropTypesstring Searcher config
Multiple Components in one page
const history = Component { const searchStr1 = thisprops return <div>searchStr1</div> } const FunctionalSearcher = { const searchStr2 = props return <div>searchStr2</div>} const config1 = history queryPropsConfig: searchStr1: QueryPropTypesstring defaultQueryProps: searchStr1: 'str1' validatorMap: searchStr1: type: ValidateTypesrange value: 'aaa' 'bbb' const config2 = history queryPropsConfig: searchStr2: QueryPropTypesstring defaultQueryProps: searchStr2: 'str2' validatorMap: searchStr2: type: ValidateTypesfunction { return val } const SearcherQueryToStateComp = const FunctionalSearcherQueryToStateComp = Component { return <ReactFragment> <SearcherQueryToStateComp /> <FunctionalSearcherQueryToStateComp /> </ReactFragment> }
updateQueryState()
After wrapping the queryToPropsHOC
, the decorated Component will have a function property updateQueryState
.
updateQueryState parameters
- First parameter: new query key-value to be updated.
- Second paramter: callback function with a new query object parameter.
const updateQueryState = thisprops
Configuration
Name | Type | Default | Description |
---|---|---|---|
history |
object |
Required . History object, see history for more information. |
|
replaceRouteWhenChange |
boolean |
true |
Optional . If true , history.replace() will be called, or history.push() will be called when query is updated by Component. |
mapDefaultQueryPropsToUrlWhenMounted |
boolean |
false |
Optional . If true , default query props will be mapped to url when Component mounted. |
queryPropsConfig |
object |
Only properties declared in the queryPropTypes object will be mapped from the path to Component props, and the declared types will be used to decode the query string to Component props. | |
defaultQueryProps |
object |
Default query props. | |
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. |
queryPropsConfig
The value of each key in queryPropsConfig
can be a QueryPropType or a function.
QueryPropTypes
- number
- string
- boolean
- array
- numericArray
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: ValidateTypesrange value: 'aa' 'bb' 'cc' key2: type: ValidateTypesregexp value: /^test/i type: ValidateTypesfunction { return val }