Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
hippo-react-router
Advanced tools
A little library for routing in React using the HTML 5 history API
NOTE: as you can guess from the above description, this only works for browsers that support the HTML 5 history API
NOTE TO SELF when you rewrite this in ts, inject routes
into the components. That way you can put get type checking on the routes, instead of it being compile time magic like it is right now. put go
and Link
in there too, why not.
npm install --save hippo-react-router
make a routes object somewhere kind of like this:
import Templates from './templates'
import Views from './views'
export default {
rootUri: '/',
template: Templates.Root,
index: { uri: '/', component: Views.Home },
login: { uri: '/login', component: Views.Login },
person: {
rootUri: '/person',
template: Templates.Person,
list: { uri: '/', component: Views.Person.List, template: false },
show: { uri: '/:personId', component: Views.Person.Show },
},
...etc
}
about the routes object:
rootUri
and template
are reserved words, sorry you won't be able to name and paths with those names. if it really ruins your day, let me know and i'll make it something more obscure.uri
field that is relative to it's placement in the object
<your website.com>/person/:personId
:personId
will be injected into the view as { personId: <the val> }
<RootTemplate routeParams={...}>
<PersonTemplate routeParams={...}>
<Person.Show routeParams={...} />
</PersonTemplate>
</RootTemplate>
template: false
to that viewimport { Router } from 'hippo-react-router'
import routes from './routes' // that file we made above ^^
/* without redux */
ReactDOM.render(
<Router routes={routes} />,
document.getElementById('root')
)
/* with redux */
ReactDOM.render(
<Provider store={store}>
<Router routes={routes} />
</Provider>,
document.getElementById('root')
)
boom, you have routes mapping to the components you defined in the object
you can import the routes object out of this library to use in your app. you should do this for 2 reasons
the aptly named go
function does what you think it does. simply pass in the route defined in your routes object, and, when called, it will go to that page
import { go, routes } from 'hippo-react-router'
go(routes.login)
// OR
go(routes.person.show, { personId: <my person id> })
this is just a convenience wrapper around the a
tag. there are 2 ways to use it:
<Link to={routes.person.show} params={myParams} ...otherProps /> // params is optional
<Link onClick={myfunc} ...otherProps /> // the same as onClick in links otherwise, except e.preventDefault() has already been called
if you don't want e.preventDefault()
to be called, you can still use <a ...>my link!</a>
, although i've never has a situation in a react app where i didn't want to intercept and prevent the default event
every view and template get injected with the following params below as this.props.routeParams
. because of this, you don't need to worry about integrating this package with redux
{
href: "/person/himom!", // the actual url of the page
params: { personId: "himom!" }, // will be an empty obj if there are no route params
query: { awesome: false }, // will be an empty obj if there are no query params
url: "/person/:personId", // the url of the page without the params replaced
urn: "person.show" // the resource name that's currently being shown
}
react-router was taken. you can check out that package too. i'm super thankful for it, as that was the package i used when i first started learning react. i think they have a fallback for browsers that don't support the HTML 5 history API (I.E 7). so you should use that library if you need legacy support.
FAQs
A router for React
The npm package hippo-react-router receives a total of 0 weekly downloads. As such, hippo-react-router popularity was classified as not popular.
We found that hippo-react-router 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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.