
Security News
Critical Security Vulnerability in React Server Components
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.
@avantstay/mobx-react-router
Advanced tools
Keep your MobX state in sync with react-router via a RouterStore.
Router location state is observable, so any references to it in MobX
components will cause the component to re-render when the location changes.
Very much inspired by (and copied from) react-router-redux.
This branch (master) is for use with react-router v4.
If you're looking for the bindings for use with react-router v3 go to the v3 branch.
npm install --save mobx-react-router
And if you haven't installed all the peer dependencies, you should probably do that now:
npm install --save mobx mobx-react react-router
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import createBrowserHistory from 'history/createBrowserHistory';
import { Provider } from 'mobx-react';
import { RouterStore, syncHistoryWithStore } from 'mobx-react-router';
import { Router } from 'react-router';
import App from './App';
const browserHistory = createBrowserHistory();
const routingStore = new RouterStore();
const stores = {
// Key can be whatever you want
routing: routingStore,
// ...other stores
};
const history = syncHistoryWithStore(browserHistory, routingStore);
ReactDOM.render(
<Provider {...stores}>
<Router history={history}>
<App />
</Router>
</Provider>,
document.getElementById('root')
);
App.js
import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
@inject('routing')
@observer
export default class App extends Component {
render() {
const { location, push, goBack } = this.props.routing;
return (
<div>
<span>Current pathname: {location.pathname}</span>
<button onClick={() => push('/test')}>Change url</button>
<button onClick={() => goBack()}>Go Back</button>
</div>
);
}
}
If you are using typescript - the built in typings for this project depend on
@types/history, so make sure you have them installed too.
Routes not updating correctly when URL changes
There is a known issue with React Router 4 and MobX (and Redux) where "blocker" components like those
created by @observer (and @connect in Redux) block react router updates from propagating down the
component tree.
There is a React Router 4 documentation page for information on this issue:
To fix problems like this, try wrapping components which are being "blocked" with React Router's withRouter higher
order component should help, depending on the case.
Refer to the link above for more information on this solution, and some alternatives.
const store = new RouterStore();
A router store instance has the following properties:
location (observable) - history location objecthistory - raw history API objectAnd the following history methods:
history - A variant of a history object, usually browserHistorystore - An instance of RouterStorereturns an enhanced history object with the following additional methods:
location observableconst unsubscribeFromStore = history.subscribe((location, action) => console.log(location.pathname));
history.push('/test1');
unsubscribeFromStore();
history.push('/test2');
// Logs
// 'test1'
history.unsubscribe();
// Store no longer updates
FAQs
Keep your MobX state in sync with react-router
We found that @avantstay/mobx-react-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.

Research
/Security News
We spotted a wave of auto-generated “elf-*” npm packages published every two minutes from new accounts, with simple malware variants and early takedowns underway.

Security News
TypeScript 6.0 will be the last JavaScript-based major release, as the project shifts to the TypeScript 7 native toolchain with major build speedups.