react-redux-provide-history
v0.0.4
Published
Provides the HTML5 History API to React components.
Downloads
5
Maintainers
Readme
react-redux-provide-history
An alternative to react-router
which provides the HTML5 History API to React components.
Installation
npm install react-redux-provide-history --save
Usage
This provides React components with the HTML5 History API via the following propTypes
:
pushWindowPath
- works exactly likewindow.history.pushState
except arguments are reversed (path
thentitle
thenstate
)replaceWindowPath
- works exactly likewindow.history.replaceState
except arguments are reversed (path
thentitle
thenstate
)setDocumentTitle
- setsdocument.title
(why? see the followingreducers
)
Your components may also be provided the following reduced props:
windowPath
- essentiallywindow.location.pathname
documentTitle
- when navigating through history, this gets updatedhistoryData
- essentiallywindow.history.state
, which is actually theaction
that triggered the navigation; avoid using this directly but it's there if you absolutely need it
A few notes:
Upon initializing the store,
replaceWindowPath
is automatically called which can be used to properly initialize the state of any other providers who make use of this provider'sREPLACE_WINDOW_PATH
action type.When navigating through history (i.e., when the
popstate
event is triggered on thewindow
), if apath
is present within thewindow.history.state
, theREPLACE_WINDOW_PATH
action type will be dispatched on top of thewindow.history.state
(stored action) to indicate that thewindow.location.pathname
has been changed; or if only atitle
is present for some reason, theSET_DOCUMENT_TITLE
action type will be dispatched instead.If you're migrating to this from
react-router
or if you want a quick and easy way to include links in your app that work both clientside and serverside, you canimport { Link } from 'react-redux-provide-history';
and use it like any othera
element.The equivalent of
router.transitionTo
is to provide thepushWindowPath
and/orreplaceWindowPath
to your components and use wherever necessary.
Example
See bloggur
and its entries
provider for a good example of how to create providers that depend on the current path. (Note: react-redux-provide
's shareStore
utility function helps when you need providers to depend on one another.)