npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-resolver

v1.0.0-beta.13

Published

react router resolver

Downloads

49

Readme

react-router-resolver

npm version npm downloads

Were you thinking about how to load data before transition or how to load it on server-side with using react-router v4? Or maybe you thought how to load chunks before transition by click the link? What about security system on your project ? Alright, you can talk that there are too many library what implements it... But I can suggest the powerful and small sized library what can help to do it on client-side and for server-side too

Alright, let's start

Contents

Internal dependencies:

| package | ver | | ------------- | ------------- | | is-browser | ^2.0.1 | | qs | 6.5.1 | | react-display-name | ^0.2.3 | | shallowequal | ^1.0.2 |

External dependencies:

| package | ver | | ------------- | ------------- | | react-router | ^4.2.0 | | react-router-config | 1.0.0-beta.4 |

Simple example

if you didn't work with the react-router-config please follow the link and review

./src/routes/index.js

import React from 'react'
import {NavLink} from 'react-router-dom'
import {renderRoutes} from 'react-router-config'


const App = ({route}) => (
    <div>
        <ul>
            <li>
                <NavLink to='/'>to home</Link>
            </li>
            <li>
                <NavLink to='/test'>to test page</Link>
            </li>
        </ul>
        {renderRoutes(route.routes)}
    </div>
)

const Test = () => 'I am test page'

export default [
    {
        path: '/',
        component: App,
        routes: [
            {
                path: '/test',
                component: Test
            }
        ]
    }
]

Ok, this is just example. let's try to add react-router-resolver to do with

We need to load data before transition to /test. Ok

export default [
    {
        path: '/',
        component: App,
        routes: [
            {
                path: '/test',
                component: Test,
                preload: async (helpers) => { // preload will wait and hold the transition until the promise is resolved or rejected
                    // here is we can to load data and put it to your storage like redux/mobx/mobx-state-tree and others...
                    helpers.dispatch({
                        type: 'PUT_DATA',
                        payload: await someActionWhichReturnsPromise()
                    })
                }
            }
        ]
    }
]

Example usage with redux

./src/resolver.js

import Resolver from 'react-router-resolver'


let resolver

const getResolver = ({store, history, routes}) => {
    if (resolver) return resolver
    resolver = new Resolver({
        routes,
        store,
        history,
        actions: {
            onStart: location => store.dispatch({
                type: 'PRELOAD_START',
                payload: location
            }),
            onSuccess: location => store.dispatch({
                type: 'PRELOAD_SUCCESS',
                payload: location
            }),
            onFail: (e, location) => store.dispatch({
                type: 'PRELOAD_FAIL',
                payload: {
                    e,
                    location
                }
            })
        },
        helpers: {
            store,
            history,
            dispatch: store.dispatch,
            getState: store.getState
            // here is you can put your own helpers ...
        },
        resolved: []
    })

    return resolver
}


export {
    getResolver
}

./src/App.jsx

import React from 'react'
import {ConnectedRouter} from 'react-router-redux'
import {renderRoutes} from 'react-router-config'
import {Provider} from 'react-redux'
import {getResolver} from './resolver'
import routes from './routes'


const App = async ({history, store}) => {
    const resolver = getResolver({store, history, routes})

    try {
        await resolver.init(history.location)
    } catch (e) {
        console.error('something went wrong', e)
    }

    return (
        <Provider store={store}>
            <ConnectedRouter history={history}>
                {renderRoutes(routes)}
            </ConnectedRouter>
        </Provider>
    )
}


export {
    App as default
}

./src/index.js

import React from 'react'
import ReactDOM from 'react-dom'
import {AppContainer} from 'react-hot-loader'
import createHistory from 'history/createBrowserHistory'
import configureStore from './store/configureStore'
import App from './App'


const history = createHistory()
const store = configureStore()


const render = App => {
    App({history, store}).then(html => {
        ReactDOM.render(
            <AppContainer warnings={false}>
                {html}
            </AppContainer>,
            document.getElementById('react-root')
        )
    })
}

render(App)

if (module.hot) module.hot.accept('./App', () => render(App))

And test it :)

API

| method | desctiption | | ------------- | ------------------------------------------------------------- | | getRoutes | returns array of passed routes to the resolver | | getResolved | returns an array of all resolved routes | | setHelpers | (helpers) setting helpers for preload/onEnter hooks | | addHelper | (key, value) add helper | | resolve | (location) resolve routes for server-side | | init | (location) resolve routes for client-side |

Helpers

import {onEnter, preload} from 'react-router-resolver'


const preloadOptions = { // this is default options
    alwaysReload: false,
    reloadOnParamsChange: true,
    reloadOnQueryChange: true
}

// also you can use decorators style
@onEnter(({check}) => check({
    authenticated: true,
    anonymous: false
}))
@preload(async ({helpers}) => {
    const data = await getSomeData()
    helpers.dispatch({
        type: 'PUT_DATA',
        payload: data
    })
}, preloadOptions)
@connect(state => state.data)
export default class Test extends React.Component {
    render() {
        const {data} = this.props
        return (
            // use data in your component
        )
    }
}

onEnter like preload but onEnter fires always when location is change

or like object

{
    path: '/test',
    component: Test,
    preload: async ({helpers}) => {
        const data = await getSomeData()
        helpers.dispatch({
            type: 'PUT_DATA',
            payload: data
        })
    },
    preloadOptions: { // this is default options
        alwaysReload: false,
        reloadOnParamsChange: true,
        reloadOnQueryChange: true
    },
    onEnter: ({check}) => check({
        authenticated: true,
        anonymous: false
    })
}