Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-router-maa
Advanced tools
state based react router linked to url through parseUrl and toUrl functions, has working route blocking control mechanisim, and async initialisation phase
State based router for react (and others not explicit to react). The idea is simple I want the url of the site to be linked to a state object. To do so the user supplies the router with a parseUrl and a toUrl function. And changing the url is done by changing the state object.
The router has a built in async (by returning a promise) initialisation phase which can be utilised by supplying the initializeRouter the initializationHandler parameter which if it return a state or a url(string) will set the initial url of the site (if it returns falsey value the initial url will not change). Note you can return a promise for async functionality. This is important for example if the initial url is say /user/:userId and you want to get the user info from the server and if no user is available with set id u want to redirect to another page say /home.
The router provides out of the box a working async route blocking mechanism available through pushTransitionAllowedCheckFunction. Where you add a function to be called before route is changed and if this function returns false the route will not change (utilise promises for async operation). Note by default once your function allows transition by returning anything but false, it (your function) will be poped from the functions to be called at route change.
The state of the router is saved internally inside the router. To hook it to your react app use something like bellow which is specific to redux (full example here):
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import rootReducer from './reducers';
import './index.css';
import App from './components/App';
import { changeRouterState, initRouter } from './actions';
import { initializeRouter } from 'react-router-maa';
import { parseUrl, toUrl } from './utils';
const store = createStore(rootReducer);
initializeRouter(parseUrl, toUrl, null, (state, position, isInit) => {
store.dispatch(changeRouterState(state, position, isInit));
}, (initState) => {
return new Promise((resolve) => {
store.dispatch(initRouter(initState, resolve));
});
});
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
I created a small app to demonstrate the router in action check example here.
I highly recommend checking the example as it helps demonstrate the power of this router in real world senarios. Even if you dont download and run the example locally checking the readme of the example is useful also.
a function that takes in a url string and returns a state object.
a function that takes in a state object and returns a url string.
a function that takes in a state object and (state) change object and returns the new state (dont mutate state or else). send it as null (or falsey in general) to use Object.assign. in most cases you'll just send this as null.
a function to be called every time the router state changed it will be given the newState as first argument, the stateLocation as the second (think of stateLocation as position in the states objects the current state is. when the site first load stateLocation is 0 if you push a new state it becomes 1), and isInit as the third argument this is set to true when the first state is set.
a function that will be called upon initialization of the router. It will be supplied the initial state which is parsed from the initial url (window.location.pathname or window.location.hash) and merged with initialNoneUrlState(another argument). If this function returns an object it will be used as the initialState instead and the url changed accordingly (using the toUrl function) or if it returns a string it will be used as as the initial url and the initial state will be parsed from it (using the parseUrl function).
The duration in milliseconds to wait before timing out the initialisation function. Set to zero (or falsey in general) to disable the timeout.
This will be passed as basename to the history object. It represents the base part of the url of your site which will be prefixed for all urls.
The initial part of the state that is not stored in the url. Will be merged with the state parsed from the url initially to get the initialState. In most cases you will not need this. so keep it as the default.
Can be set to either 'browser', 'hash', or 'memory' selects the underlying history create function to be used.
Returns whether or not the router is initialised.
a function that pushes a new state to the router and changes the url accordingly.
The new state to push.
will be forwarded to history.push as the second argument. Mostly you wont need this so dont send it.
Returns a promise that resolved to the new state if the navigation was not blocked, or a promise that resolves to explicitly false if the navigation was blocked.
a function that pushes a change to the current the router state and changes the url accordingly. The change object is merged with the current state using the mergeRouterStateChangeFunction(which defaults to Object.assign).
The change object to merge with the current state.
will be forwarded to history.push as the second argument. Mostly you wont need this so dont send it.
Returns a promise that resolved to the new state if the navigation was not blocked, or a promise that resolves to explicitly false if the navigation was blocked.
Same as pushRouterState but replaces current state instead of pushing new one.
Same as pushRouterStateThroughChange but replaces current state instead of pushing new one.
Goes back or forward in router states.
The change to move. -1 to go back one step, 1 to move forward one step. 0 will throw an error.
Returns the router state delta steps away (same concept of delta in goInRouterStates above). Not sending a delta or sending a zero as delta will return the current state, which is what is needed in most cases.
-1 returns previous state, 1 returns next (forward) state. 0 or nothing will return the current state.
Returns a the state at delta offset from current or undefined if it doesnt exist.
Returns current stateLocation
Returns current stateLocation same as the one sent to routerStateChangedHandler explained above.
Returns length of the states object.
Returns length of the states object. E.g. if you navigated 10 times this will be 10.
pushes a transition allowed checkk function.
A function to be called to check if transition is to be allowed or not returning true (or anything other than explicitly false) will allow transition returning false will block transition. note: this function can be async i.e. returning a promise that resolves to true or false has the same effect but is async. also note that by default when this function allows transition (like returns anything but false) it will be poped (removed from the check call stack). to disable this behaviour send popOncePasses (the second argument) as false. This is done specifically like this cause most of the time you push a function for example at pageLoad/componentDidLoad to block transition if some changes have not been saved and once the user confirms or there is no changes anyways, you want this function check to be removed. Also note that only the top most function is called not all the functions in the stack (it made more sence to me).
If set to false will not remove the transitionAllowedCheckFunction when it allows the transition. See argument transitionAllowedCheckFunction description above.
FAQs
state based react router linked to url through parseUrl and toUrl functions, has working route blocking control mechanisim, and async initialisation phase
The npm package react-router-maa receives a total of 2 weekly downloads. As such, react-router-maa popularity was classified as not popular.
We found that react-router-maa demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.