Socket
Socket
Sign inDemoInstall

@pubcore/redux-browser-history

Package Overview
Dependencies
5
Maintainers
3
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @pubcore/redux-browser-history

maintain browser URI in state in redux style


Version published
Weekly downloads
32
increased by128.57%
Maintainers
3
Install size
612 kB
Created
Weekly downloads
 

Readme

Source

Maintain browser's URI in state based on redux

Prerequisites

  • Knowledge of redux-actions and -reducers
  • thunk-middleware is applied to redux-store

Features

  • Supports arbitrary sliceKey (default is "uri")
  • Supports base-path, initialized by a carrier on import of reducer
  • Updates state, if browser-uri changes
  • Offers action to update browser's URI and/or query params
  • Offers param "replace", if true history's "replaceState" is used.
  • Adds origin if needed for location.href

Example

State
{
 	uri:{
		basePath:'/todolist'
		subPath:'/items',
		query:{
			id: 1
		}
	}
}
app's main
import {createStore, combineReducers, applyMiddleware} from 'redux'
import thunkMiddleware from 'redux-thunk'
import uri, {listenUri, init} from '@pubcore/redux-browser-history'

// optional, required if key is not 'uri' and not on first level of state
// init({sliceKey:'ui.uri'})

var {dispatch, getState} = createStore(combineReducers({
	uri
}), {}, applyMiddleware(thunkMiddleware))

//watch changes of browser's uri
listenUri(dispatch)
action to navigate between app's screens
import {updateUriAction} from '@pubcore/redux-browser-history'

export default navigate = page => dispatch => {
	dispatch(updateUriAction({subPath: '/' + page}))
	//...
}
update some query params
import get from 'lodash.get'
import {updateUriAction} from '@pubcore/redux-browser-history'
//[...]

const current = get(getState(), 'uri.query')
//[...]

//updateUriAction's query param must be "complete" => ther 's no merge
updateUriAction({ query:{...current, id: 3} })

// to remove all query params
// updateUriAction({ query:{} })

// to remove some query params, use "undefined"
// updateUriAction({ query:{..current, foo: undefined, bar: undefined} })

// to use history's replaceState (instead of pushState)
// updateUriAction({ query:{}, replace:true})

Keywords

FAQs

Last updated on 01 Nov 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc