with-react-query

1.4.1 • Public • Published

with-react-query

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

CircleCI npm version

Basic usage with params

// Let's say you are at location '/foo?counter=1'
import React, { PureComponent } from 'react'
import { withRouter } from 'react-router-dom'
import withReactQuery from 'with-react-query'

class FooPage extends PureComponent {

  onIncrementCounter = () => {
    const { history, query } = this.props
    const { getSearchFromUpdate, params: { counter } } = query
    // navigate to /foo?counter=2
    history.push(getSearchFromUpdate({ counter: counter + 1 }))
  }

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

export default withRouter(withReactQuery()(FooPage))

Usage for url in foreign language with translate

// Let's say you are at location '/foo/compteur=1'
import React, { PureComponent } from 'react'
import { withRouter } from 'react-router-dom'
import withReactQuery from 'with-react-query'

class FooPage extends PureComponent {

  onIncrementCounter = () => {
    const { history, query } = this.props
    const { counter } = query.getTranslatedParams()
    // navigate to /foo?compteur=2
    history.push(query.getSearchFromUpdate({ counter: counter + 1 }))
  }

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

export default withRouter(withReactQuery({
  mapper: { compteur: "counter" }
})(FooPage))

Package Sidebar

Install

npm i with-react-query

Weekly Downloads

2

Version

1.4.1

License

MPL-2.0

Unpacked Size

33.8 kB

Total Files

9

Last publish

Collaborators

  • ledoux