question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add query params to current url

See original GitHub issue

I want to add query param to current url, I don’t know what current url is.

For example I have /token/ETH or /user/user1 urls and want to change they to /token/ETH?m=ref or /user/user1?m=ref accordingly.

Is it possible to define an action only for updating query in URL?

Will appreciate any help

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
hedgepigdanielcommented, Aug 25, 2018

The current route action is available in the location reducer, so its quite simple to write a thunk which copies the current route action, changes some part of it, and dispatches it.

0reactions
wasinsandiegocommented, Oct 10, 2019

I know it’s closed but closed issues are still useful when people are looking for solutions, right? In case it’s useful for someone else…

This is my higher order component (using recompose and connect) that adds a function prop named queryUpdate to any component that will dispatch an action to update the query without a route change. You could modify to create a react hook or a HOC without recompose.

import { connect } from 'react-redux'
import { compose, withHandlers } from 'recompose'
// my selectors to get things from location slice
import {
  locationTypeSelector,
  locationPayloadSelector,
  locationQuerySelector
} from 'routes.selectors'

export const updateQuery = ({ type, payload, query }) => ({
  type, payload, query
})

const mapStateToProps = state => ({
  locationType: locationTypeSelector(state),
  locationPayload: locationPayloadSelector(state),
  locationQuery: locationQuerySelector(state)
})

const mapDispatchToProps = {
  updateQuery
}

export const withQueryUpdate = compose(
  connect(mapStateToProps, mapDispatchToProps),
  withHandlers({
    queryUpdate: ({ updateQuery, locationType, locationPayload, locationQuery }) => ({ query }) => {
      updateQuery({
        type: locationType,
        payload: locationPayload,
        query: { ...locationQuery, ...query }
      })
    }
  })
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add url parameter to the current url? - Stack Overflow
There is no way to write a relative URI that preserves the existing query string while adding additional parameters to it. You have...
Read more >
How to Get and Set Query Parameters From URL - getButterfly
Here are two ways to get (and set) query parameters from URL. ... Construct URLSearchParams object instance from current URL querystring let ...
Read more >
Php: Replace or add query parameter to the current url
phpReplace or add query parameter to the current url. $url = parse_url($_SERVER['REQUEST_URI']); parse_str($url['query'], $q); $params = ['a' => 1, ...
Read more >
URLSearchParams.append() - Web APIs - MDN Web Docs
URLSearchParams.append(). The append() method of the URLSearchParams interface appends a specified key/value pair as a new search parameter.
Read more >
Angular Basics: Using Query Params in Angular Routing
See the query parameters from search and other queries too merged with the “&” character. Angular makes it easy for us to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found