4.2.0 • Published 1 year ago

mobx-react-router-utils v4.2.0

Weekly downloads
346
License
ISC
Repository
github
Last release
1 year ago

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)
    }

    // ...
}
3.2.0

1 year ago

4.2.0

1 year ago

4.1.4

1 year ago

4.1.3

2 years ago

4.1.2-rc1

2 years ago

4.1.2

2 years ago

4.1.0-rc1

2 years ago

3.3.0-rc1

2 years ago

4.1.0

2 years ago

4.1.1

2 years ago

4.0.0

3 years ago

3.1.9

4 years ago

3.1.8

4 years ago

3.1.7

4 years ago

3.1.6

4 years ago

3.1.5

4 years ago

3.1.3

4 years ago

3.1.4

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.8

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.0.19

4 years ago

1.1.0

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.9

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.27

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago