Coder Social home page Coder Social logo

redux-data-router's Introduction

CI npm version

โš™๏ธ redux-data-router ๐Ÿ”ฉ

redux-data-router is a clean-room spiritual successor to projects like redux-first-history.

You only need to bring your React Router v6 data router, no need to manage a history object (or use UNSTABLE_ methods ๐Ÿ˜„)

Usage with router

import {createEnhancer, reducer} from 'redux-data-router';

// Create your data router like normal.
const router = createBrowserRouter(routes);
// Install the router reducer and enhancer.
const store = configureStore(
  reducer: {router: reducer}, // Install the reducer at the 'router' slice by default.
  enhancers: [createEnhancer(router)],
);

Redux is now connected to React Router! Yes, that's it.

Backwards-compatible

Effort has been made to keep the history action types working. redux-data-router exports all the actions you know and love:

dispatch(back());
dispatch(forward());
dispatch(go(2));
dispatch(goBack(2));
dispatch(goForward());
dispatch(push('/', state || {}));
dispatch(replace('/about'));

There is a new navigate action that is more semantically in line with how the modern router actually works:

dispatch(navigate('/about'));
dispatch(navigate('/about', {replace: true}));
dispatch(navigate(2));

Under the hood

History actions are casted to an equivalent navigate action. See: actions.js

Does this support time-travelling?

Yes. ๐Ÿ˜ Router navigation is a side-effect of every location state change.

Configuration

createEnhancer takes an optional options object after your router:

{
  // The key used for the router slice.
  key: 'router',
}

For example, the introduction could have been configured differently for your unique state structure:

const store = configureStore(
  reducer: {FOO_BAR: reducer},
  enhancers: [createEnhancer(router, {key: 'FOO_BAR'})],
);

How it works

Up to now, libraries have been handling a history object to synchronize Redux state with React Router. This approach is no longer practical with current (February 2024) React Router data router semantics.

All is not lost, however! Instead of history.listen, we have a new API: router.subscribe. redux-data-router subscribes to changes through this channel to continuously synchronize the Redux state with the router state.

Is this a real single source of truth?

Probably not. React Router's internals aren't always easy to get into, and there may even be issues with the approach taken here. If e.g. the internal updateState API were made public, we might be able to become a true SSOT.

Contributing

npm ci
npm run lint && npm run test

redux-data-router's People

Contributors

cha0s avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.