React URL Query
A library for managing state through query parameters in the URL in React. It integrates well with React Router and Redux and provides additional tools specifically targeted at serializing and deserializing state in URL query parameters.
For details on how to use it, read the docs or browse the examples.
Installation
npm install --save react-url-query
How do I use it?
A number of examples have been created demonstrating a variety of methods of using the library with different technologies. Here is the most basic form of using it in a component:
import React, { PureComponent, PropTypes } from 'react';
import { addUrlProps, UrlQueryParamTypes } from 'react-url-query';
const urlPropsQueryConfig = {
bar: { type: UrlQueryParamTypes.string },
foo: { type: UrlQueryParamTypes.number, queryParam: 'fooInUrl' },
};
class MainPage extends PureComponent {
static propTypes = {
bar: PropTypes.string,
foo: PropTypes.number,
onChangeFoo: PropTypes.func,
onChangeBar: PropTypes.func,
}
static defaultProps = {
foo: 123,
bar: 'bar',
}
render() {
const { foo, bar, onChangeFoo, onChangeBar } = this.props;
return (
<div>
<div>
foo={foo}
<button onClick={() => onChangeFoo(999)}>Set foo to 999</button>
</div>
<div>
bar={bar}
<button onClick={() => onChangeBar('testing')}>
Set bar to "testing"
</button>
</div>
</div>
);
}
}
export default addUrlProps({ urlPropsQueryConfig })(MainPage);
If you prefer, instead of using a urlPropsQueryConfig
you can provide the functions mapUrlToProps
and mapUrlChangeHandlersToProps
, as shown in the basic-mapUrlToProps example.
You'll also need to configure which history
to use, typically done wherever you initialize your application. Examples of doing this with different setups are shown in the examples section.
If you are using react-router, how you link in the history depends on the version.
React Router v2
import { configureUrlQuery } from 'react-url-query';
import { browserHistory } from 'react-router'
configureUrlQuery({ history: browserHistory });
React Router v4
import { RouterToUrlQuery } from 'react-url-query';
import Router from 'react-router/BrowserRouter';
ReactDOM.render(
<Router>
<RouterToUrlQuery>
<App />
</RouterToUrlQuery>
</Router>,
document.getElementById('root')
);
Not using React Router. If you're not using react-router, you'll need to instantiate the history yourself manually:
import { configureUrlQuery } from 'react-url-query';
import createHistory from 'history/createBrowserHistory';
const history = createHistory();
configureUrlQuery({ history });
Examples
Development
During development of examples, it can be helpful to have a watch running automatically rebuilding the package when changes take place. To get this running run:
npm run dev
Building
npm run build
Linting
npm run lint
To lint examples, run:
npm run lint:examples
Testing
npm run test
To test examples, run:
npm run test:examples
Working on docs
When editing the docs, it helps to have a dev server watching changes. To do this, run:
npm run docs:watch
To build the docs, run:
npm run docs:build
To publish the docs, run:
npm run docs:publish
License
MIT