react-history-lite
provides tools to manage session history using React
. This is a simple wrapper of history
library, allows you to develop a SPA React
project without install the whole react-router
.
Note: This project is suit for small SPA, if you need to handle routes between different components, react-router
is your best choice.
https://waynelai614.github.io/react-history-lite/
The provider component store the history
data in the context.
Wrap with your App
component, depending on where you want to keep track of history changes.
import { HistoryContext } from 'components/history'
const App = () => {
<HistoryContext.Provider>
<App />
</HistoryContext.Provider>
}
The component allows you to do some side effect when location changes.
A selector function to extract params data from location
, and pass as second argument to onChange()
.
Default function:
Return the url params (From "?a=1&b=2"
to { a: '1', b: '2' }
)
You can build your custom param selector e.g.
const paramsSelector = location => {
const {
pathname, search, hash
} = location
return {
// ...
}
}
A callback function allows you to do some side effect when location changes
e.g.
// argument `params` is the output of `paramsSelector()`
const onChange = ({ location, params }) => {
const {
pathname, search, hash
} = location
// do something... ex: dispatch redux action, call api, etc.
}
import { HistoryContext, Location } from 'components/history'
const Parent = () => {
<HistoryContext.Provider>
<Child />
</HistoryContext.Provider>
}
const Child = () => (
<div>
<p>Child Component</p>
<Location
paramsSelector={customParamsSelector}
onChange={handleLocationChange}
/>
</div>
)
The hoc
version of <Location />
import {
compose, withHandlers, pure
} from 'recompose'
import { withLocation } from 'components/history'
const enhance = compose(
withHandlers({
handleLocationChange: props => ({ params }) => {
// ...
},
customParamsSelector: props => location => {
return {
// ...
}
}
}),
withLocation(({ customParamsSelector, handleLocationChange }) => ({
paramsSelector: customParamsSelector,
onChange: handleLocationChange
})),
)
A hoc
allows you to get <HistoryContext.Provider />
's context value as historyCtx
prop to child component
import { connectHistory } from 'components/history'
const Foo = ({ historyCtx }) => {
// historyCtx - <HistoryContext.Provider />'s context
}
export default connectHistory(Foo)
Use the history
object methods to change the current location.
Please see ReactTraining/history#navigation for more details.
import { $history } from 'utils/history'
const doSearch = (params = {}) => {
const { keyword } = params
const searchUrl = `/?keyword=${keyword}`
$history.push(searchUrl)
}
const SearchButton = () => (
<button
type="button"
onClick={() => doSearch({ keyword: 'iPhone' })}
>
Search iPhone
</button>
)
- Clone this repository
git clone https://github.com/waynelai614/react-history-lite.git
- Install packages
npm install
- Run the app
npm run dev
Visit http://0.0.0.0:3000