mobx-react-router-utils
TypeScript icon, indicating that this package has built-in type declarations

4.2.0 • Public • Published

MobX React Router Utils

Routing utils to use with RouterStore (from mobx-react-router) on your MobX stores.

Install it

yarn add mobx-react-router-utils

Use it

import { computedRouteParam, setRoutingStore } from 'mobx-react-router-utils'

// Set the reference to your routing store before any usage
// You ony need to this once in your application 
setRoutingStore(routingStore)

const routes = {
  search: '/search',
  citySearch: '/cities/:city'
}

class DemoSearchStore {
    city = computedRouteParam('city', {
      patterns: [routes.citySearch],
    })
    
    // this one will come from the query string
    checkIn = computedRouteParam('checkIn', {
      // Define the route patterns for which this one is valid
      patterns: [routes.search, routes.citySearch],
      
      // Set a parsing fn to transform from string
      parse: _parseDate,
      
      // Set a formatting fn to transform into string
      format: _formatDate, 

      // It'll return the default value in case it's 
      // not present on the route or with an empty value
      defaultValue: 'temecula'
    })
    
    checkOut = computedRouteParam('checkOut', {
      patterns: [routes.search, routes.citySearch],
      parse: _parseDate,
      format: _formatDate,
    })
    
    setCity = (city: Maybe<string>) => {
      // For updating the current route, push() and replace() methods are available:
      // - push: will add the new url to the browsing history
      // - replace: will replace the current url on the browsing history
      this.city.push(city, {
        // enforce a route pattern if needed, otherwise, it will use the 
        // current location (if matches the config) or first pattern available
        pattern: !city && routes.search,
        
        // cleanup all other params (default: false)
        cleanParams: true,  
              
        // OR cleanup only selected params
        cleanParams: [   
          this.checkIn, 
          this.checkOut
        ]
      })
    }
    
    setPeriod = (checkIn: Maybe<Date>, checkOut: Maybe<Date>) => {
      this.checkIn.push(checkIn)
      this.checkOut.replace(checkOut)
    }

    // ...
}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.2.017latest

Version History

VersionDownloads (Last 7 Days)Published
4.2.017
3.2.059
4.1.40
4.1.39
4.1.20
4.1.2-rc10
4.1.10
4.1.00
4.1.0-rc10
3.3.0-rc10
4.0.00
3.1.90
3.1.80
3.1.70
3.1.60
3.1.50
3.1.40
3.1.30
3.1.10
3.1.00
3.0.80
3.0.60
3.0.50
3.0.40
3.0.30
3.0.21
3.0.10
3.0.00
2.1.00
2.0.30
2.0.20
2.0.10
2.0.00
1.3.00
1.2.00
1.1.00
1.0.270
1.0.260
1.0.250
1.0.240
1.0.230
1.0.220
1.0.210
1.0.200
1.0.190
1.0.180
1.0.170
1.0.160
1.0.150
1.0.140
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.10
1.0.00

Package Sidebar

Install

npm i mobx-react-router-utils

Weekly Downloads

74

Version

4.2.0

License

ISC

Unpacked Size

38.4 kB

Total Files

18

Last publish

Collaborators

  • wellguimaraes
  • zibra