Installations
npm install redux-query-sync-tx
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
8.11.3
NPM Version
5.6.0
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Treora
Download Statistics
Total Downloads
429
Last Day
1
Last Week
2
Last Month
7
Last Year
58
GitHub Statistics
79 Stars
61 Commits
24 Forks
5 Watching
1 Branches
5 Contributors
Bundle Size
16.82 kB
Minified
5.87 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.1.9
Package Id
redux-query-sync-tx@0.1.9
Unpacked Size
18.43 kB
Size
6.08 kB
File Count
4
NPM Version
5.6.0
Node Version
8.11.3
Total Downloads
Cumulative downloads
Total Downloads
429
Last day
0%
1
Compared to previous day
Last week
100%
2
Compared to previous week
Last month
250%
7
Compared to previous month
Last year
-15.9%
58
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
3
redux-query-sync
Treat the URL query parameters as exposed variables of your Redux state. For example,
/mypage.html?p=14
could correspond to a state object containing {pageNumber: 14}
.
Any changes to the store state are reflected in the URL. Vice versa, if the URL is changed using the
history
module, the changed parameters are updated in the store state.
Alternatives
Similar modules exist, which you might prefer in some scenarios:
- If you already use React Router, also have a look at react-router-redux-sync. If you
prefer combining React Router with redux-query-sync, ensure you use
Router
(notBrowserRouter
) and pass both modules the samehistory
instance (see this explanation). - If you use React without Redux, try react-url-query.
Otherwise, keep reading.
Install
npm install redux-query-sync
Usage
As a minimal example, let's say we want to synchronise query parameter dest
with the value of the
state's route.destination
field, to parse/make URLs such as directions.html?dest=Amsterdam
.
Minimal example
1import ReduxQuerySync from 'redux-query-sync'
2
3ReduxQuerySync({
4 store, // your Redux store
5 params: {
6 dest: {
7 // The selector you use to get the destination string from the state object.
8 selector: state => state.route.destination,
9 // The action creator you use for setting a new destination.
10 action: value => ({type: 'setDestination', payload: value}),
11 },
12 },
13 // Initially set the store's state to the current location.
14 initialTruth: 'location',
15})
Note that redux-query-sync does not modify the state, but lets you specify which action to dispatch when the state should be updated. It does modify the location (using history.pushState/replaceState), but ensures to only touch the parameters you specified.
Let's look at a more elaborate example now. We sync the query parameter p
with the value of the
state's pageNumber
field, which includes a mapping between string and integer.
Full example
1import ReduxQuerySync from 'redux-query-sync'
2
3ReduxQuerySync({
4 store,
5 params: {
6 p: {
7 selector: state => state.pageNumber,
8 action: value => ({type: 'setPageNumber', payload: value}),
9
10 // Cast the parameter value to a number (we map invalid values to 1, which will then
11 // hide the parameter).
12 stringToValue: string => Number.parseInt(string) || 1,
13
14 // We then also specify the inverse function (this example one is the default)
15 valueToString: value => `${value}`,
16
17 // When state.pageNumber equals 1, the parameter p is hidden (and vice versa).
18 defaultValue: 1,
19 },
20 },
21 initialTruth: 'location',
22
23 // Use replaceState so the browser's back/forward button will skip over these page changes.
24 replaceState: true,
25})
Note you could equally well put the conversion to and from the string in the selector and action
creator, respectively. The defaultValue
should then of course be a string too.
Working example
J-F-Far made a working example, using redux-query-sync in the app template generated by create-react-app. You can see how it configures redux-query-sync in src/index.js.
API
ReduxQuerySync()
Sets up bidirectional synchronisation between a Redux store and window location query parameters.
Param | Type | Description |
---|---|---|
options.store | Object | The Redux store object (= an object {dispatch, getState} ). |
options.params | Object | The query parameters in the location to keep in sync. |
options.params[].action | function: value => action | The action creator to be invoked with the parameter value. Should return an action that sets this value in the store. |
options.params[].selector | function: state => value | The function that gets the value given the state. |
[options.params[].defaultValue] | * | The value corresponding to absence of the parameter. You may want this to equal the state's default/initial value. Default: undefined . |
[options.params[].valueToString] | function | Specifies how to cast the value to a string, to be used in the URL. Defaults to javascript's automatic string conversion. |
[options.params[].stringToValue] | function | The inverse of valueToString. Specifies how to parse the parameter's string value to your desired value type. Defaults to the identity function (i.e. you get the string as it is). |
options.initialTruth | string | If set, indicates whose values to sync to the other, initially. Can be either 'location' or 'store' . If not set, the first of them that changes will set the other, which is not recommended. Usually you will want to use location . |
[options.replaceState] | boolean | If truthy, update location using history.replaceState instead of history.pushState , to not add entries to the browser history. Default: false |
[options.history] | Object | If you use the 'history' module, e.g. when using a router, pass your history object here in order to ensure all code uses the same instance. |
Returns: a function unsubscribe()
that can be called to stop the synchronisation.
ReduxQuerySync.enhancer()
For convenience, one can set up the synchronisation by passing an enhancer to createStore.
Example
1const storeEnhancer = ReduxQuerySync.enhancer({ 2 params, 3 initialTruth, 4 replaceState, 5}) 6const store = createStore(reducer, initialState, storeEnhancer)
Arguments to ReduxQuerySync.enhancer
are equal to those for ReduxQuerySync
itself, except that
store
can now of course be omitted. With this approach, you cannot cancel the synchronisation.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: Licence:0
- Info: FSF or OSI recognized license: The Unlicense: Licence:0
Reason
Found 4/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 7 are checked with a SAST tool
Reason
25 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
Score
1.9
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More