# with-query-router

> A small wrapper of react-router parsing the query params from the location.search

Latest version **3.3.1** (published 2019-09-03) · MPL-2.0 license · 0 weekly downloads

## Install

```sh
npm install with-query-router
pnpm add with-query-router
yarn add with-query-router
bun add with-query-router
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.3.1 |
| Published | 2019-09-03 |
| First published | 2019-02-04 |
| Weekly downloads | 0 |
| License | MPL-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 78.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 1 |
| Maintainers | ledoux |
| Keywords | react, react-router, hoc, query |

## Links

- npm: https://www.npmjs.com/package/with-query-router
- Repository: https://github.com/betagouv/with-query-router
- Homepage: https://github.com/betagouv/with-query-router#readme
- Issues: https://github.com/betagouv/with-query-router/issues
- npm.io page: https://npm.io/package/with-query-router

## Dependencies (10)

- [react](https://npm.io/package/react.md) ^16.9.0
- [reselect](https://npm.io/package/reselect.md) ^3.0.1
- [react-dom](https://npm.io/package/react-dom.md) ^16.9.0
- [classnames](https://npm.io/package/classnames.md) 2.2.5
- [prop-types](https://npm.io/package/prop-types.md) ^15.6.2
- [lodash.uniq](https://npm.io/package/lodash.uniq.md) ^4.5.0
- [re-reselect](https://npm.io/package/re-reselect.md) ^2.1.0
- [query-string](https://npm.io/package/query-string.md) ^5.1.1
- [lodash.invert](https://npm.io/package/lodash.invert.md) ^4.3.0
- [react-router-dom](https://npm.io/package/react-router-dom.md) ^5.0.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.3.1 (latest) — 2019-09-03
- 3.3.0 — 2019-09-01
- 3.2.1 — 2019-07-02
- 3.2.0 — 2019-06-20
- 3.1.0 — 2019-05-08
- 3.0.0 — 2019-04-10
- 2.10.0 — 2019-04-10
- 2.9.1 — 2019-04-03
- 2.9.0 — 2019-04-02
- 2.8.0 — 2019-04-02
- 2.5.1 — 2019-04-02
- 2.7.0 — 2019-03-29
- 2.6.0 — 2019-03-29
- 2.5.0 — 2019-03-28
- 2.4.1 — 2019-03-28
- … 16 more at https://npm.io/package/with-query-router/versions

## README

# with-query-router

A small wrapper of react-router parsing the query params from the location.search

[![CircleCI](https://circleci.com/gh/betagouv/with-query-router/tree/master.svg?style=svg)](https://circleci.com/gh/betagouv/with-query-router/tree/master)
[![npm version](https://img.shields.io/npm/v/with-query-router.svg?style=flat-square)](https://npmjs.org/package/with-query-router)

## Basic usage with `parse`
```javascript

// Let's say you are at location '/foo?counter=1'

import withQueryRouter from 'with-query-router'

class FooPage extends Component {

  onIncrementCounter = () => {
    const { query } = this.props
    const { counter } = query.parse()
    // navigate to /foo?counter=2
    query.change({ counter: counter + 1 })
  }

  render () {
    const { query } = this.props
    const { counter } = query.parse()
    return (
      <div>
        My counter is equal to {counter}
        <button onClick={this.onIncrementCounter}>
          Increment
        </button>
      </div>
    )
  }
}

export default withQueryRouter()(FooPage)
```

## Usage for creation and edition with `context`
```javascript

import { Field, Form } from 'react-final-form'
import { requestData } from 'redux-saga-data'
import withQueryRouter from 'with-query-router'

class FooPage extends Component {

  handleActivateForm = () => {
    const { foo, query } = this.props
    const { id } = foo
    // creation with pathname context
    history.push('new')
    // edition with pathname context
    query.change({ edit: null })
    // creation|edition with search context
    query.change({ foo: id
      ? `edit${id}`
      : 'new'
    })
  }

  handleDeactivateForm = formResolver => () => {
    const { history } = this.props
    const { originLocationString } = query.context()
    formResolver()
    history.push(originLocationString)
  }

  onFormSubmit = formValues => {
    const { dispatch, foo, history, query } = this.props
    const { id } = (foo || {})
    const { originLocationString } = query.context()
    const formSubmitPromise = new Promise(resolve => {
      dispatch(requestData({
        apiPath: `/foos/${id || ''}`,
        body: { ...formValues },
        handleSuccess: this.handleDeactivateForm(resolve),
        method
      }))
    })
    return formSubmitPromise
  }

  render () {
    const { query } = this.props
    const { isReadOnly } = query.context()
    return (
      <Form
        initialValues={initialValues}
        onSubmit={this.onFormSubmit}
        render={() => (
          <form onSubmit={handleSubmit}>
            <Field
              name={name}
              render={({ input, meta }) => (
                <input
                  {...input}
                  readOnly={readOnly}
                  type="text"
                />
              )}
            />
            {
              readOnly
              ? (
                <button onClick={this.handleActivateForm} type="button">
                  Create or Edit
                </button>
              )
              : (
                <button type="submit">
                  Save
                </button>
              )
            }
          </form>
        )}
      />
    )
  }
}

export default withQueryRouter()(FooPage)
```

## Usage for url in foreign language with `translate`
```javascript

// Let's say you are at location '/foo/compteur=1'

import withQueryRouter from 'with-query-router'

class FooPage extends Component {

  onIncrementCounter = () => {
    const { query } = this.props
    const { counter } = query.translate()
    // navigate to /foo?compteur=2
    query.change({ counter: counter + 1 })
  }

  render () {
    const { query } = this.props
    const { counter } = query.translate()
    return (
      <div>
        My counter is equal to {counter}
        <button onClick={this.onIncrementCounter}>
          Increment
        </button>
      </div>
    )
  }
}

export default withQueryRouter({
  mapper: { compteur: "counter" }
})(FooPage)
```

---
_Source: https://npm.io/package/with-query-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
