mobx-location

0.6.6 • Public • Published

mobx-location

a browser location as a mobx observable. Minimal wrapper around browser history utilizing popstate event and HTML5 history api. Package history-events is used for monkeypatching.

Prime usage is in your mobx observers. You can directly access the location and your app will rerender itself without the need for react-router or similar solution.

import makeMobxLocation from './mobx-location'
import { autorun, toJS } from 'mobx'
 
const mobxLocation = makeMobxLocation({ hashHistory: false }) // default
const mobxLocation = makeMobxLocation({ hashHistory: true }) // when you use hash history instead of html5 history APIs
 
const mobxLocation = makeMobxLocation({ arrayFormat: 'bracket' }) // default
const mobxLocation = makeMobxLocation({ arrayFormat: 'index' }) // will index array params in the url, refer to https://www.npmjs.com/package/query-string#arrayformat
 
autorun(() => {
  toJS(location) // runs every time browser location changes
})

location has all the standard properties:

hash
host
hostname
href
origin
pathname
port
protocol
search

plus one extra- query. Query is just parsed location.search. Query is always there even if no search params are in your location.

If you modify the query object, it will propagate back to the window location so you don't have to construct the search params yourself when modifying. Keep in mind that any newly added property in query won't be picked up-you must use set from import {set} from 'mobx' to set new props.

Browser support

same as html5 history api-so with that shim even IE9

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.6.6112latest

Version History

VersionDownloads (Last 7 Days)Published
0.6.6112
0.6.50
0.6.40
0.6.30
0.6.20
0.6.10
0.6.00
0.5.20
0.5.10
0.5.00
0.4.00
0.3.10
0.3.00
0.2.00
0.1.10
0.1.00

Package Sidebar

Install

npm i mobx-location

Weekly Downloads

83

Version

0.6.6

License

MIT

Unpacked Size

1.18 MB

Total Files

20

Last publish

Collaborators

  • capaj