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

connected-react-router-lite

v0.0.1-alpha

Published

don't use it yet :)

Downloads

3

Readme

Work in progress

don't use it yet :)

Connected React Router Lite

A fork of connected-react-router to support react-router v6 with reduced API surface.

Originally created by Supasate Choochaisri

Supported features

  • Synchronize router state with redux store through uni-directional flow (i.e. history -> store -> router -> components).

  • Supports React Router v6.

  • Dispatching of history methods (push, replace, go, back, forward) works for both redux-thunk and redux-saga.

  • Supports time traveling in Redux DevTools.

  • Supports TypeScript

Removed features

Installation

Connected React Router requires React 17 and React Redux 7.1 or later.

$ npm install --save connected-react-router-lite

Or

$ yarn add connected-react-router-lite

Usage

Step 1

In your root reducer file,

  • Create a function that takes history as an argument and returns a root reducer.
  • Add router reducer into root reducer by passing history to createRouterReducer.
  • Note: The key MUST be 'router'.
// reducers.js
import { combineReducers } from "redux";
import { createRouterReducer } from "connected-react-router-lite";
import { History } from "history";

export const createRootReducer = (history: History) => combineReducers({
  router: createRouterReducer(history),
  ... // rest of your reducers
});

Step 2

When creating a Redux store,

  • Create a history object.
  • Provide the created history to the root reducer creator.
  • Use routerMiddleware(history) if you want to dispatch history actions (e.g. to change URL with push('/path/to/somewhere')).
// configureStore.js
...
import { createBrowserHistory } from "history";
import { applyMiddleware, compose, createStore } from "redux";
import { routerMiddleware } from "connected-react-router-lite";
import { createRootReducer } from "./reducers";
...
export const history = createBrowserHistory();

export function configureStore(preloadedState) {
  const rootReducer = createRootReducer(history);
  const store = createStore(
    rootReducer,
    preloadedState,
    compose(
      applyMiddleware(
        routerMiddleware(history), // for dispatching history actions
        // ... other middlewares ...
      ),
    ),
  );
  return store;
}

Step 3

  • Wrap your react-router v6 routing with ConnectedRouter and pass the history object as a prop.
  • Place ConnectedRouter as a child of react-redux's Provider.
  • N.B. If doing server-side rendering, you should still use the StaticRouter from react-router on the server.
// index.js
...
import { Provider } from "react-redux";
import { Route, Routes } from "react-router";
import { ConnectedRouter } from "connected-react-router-lite";
import { configureStore, history } from "./configureStore";
...
const store = configureStore();

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <>
        <Routes>
          <Route exact path="/" render={<div>Match</div>} />
          <Route element={<div>Miss</div>} />
        </Routes>
      </>
    </ConnectedRouter>
  </Provider>,
  document.getElementById("react-root"),
);

Note: the history object provided to router reducer, routerMiddleware, and ConnectedRouter component must be the same history object.

Now, it's ready to work!

Build

yarn build

Generated files will be in the lib folder.

Development

When testing the example apps with npm link or yarn link, you should explicitly provide the same Context to both Provider and ConnectedRouter to make sure that the ConnectedRouter doesn't pick up a different ReactReduxContext from a different node_modules folder.

In index.js.

...
import { Provider, ReactReduxContext } from 'react-redux'
...
      <Provider store={store} context={ReactReduxContext}>
        <App history={history} context={ReactReduxContext} />
      </Provider>
...

In App.js,

...
const App = ({ history, context }) => {
  return (
    <ConnectedRouter history={history} context={context}>
      { routes }
    </ConnectedRouter>
  )
}
...

Contributors

See Contributors and Acknowledge.

License

MIT License