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.

Calling a function in a component when a query parameter changes

See original GitHub issue

Right now I’m working on moving some of our component state into query parameters so that pages are bookmarkable and other components can cleanly change the values.

It looks like react-router will call .render when a query parameter changes, but it’s not clear how to tell which part of the URL / which query parameter changed, and the work I’m doing is outside of .render, in an onChange or componentDidUpdate lifecycle method.

Is there a clean way of implementing this? It seems like I either need to:

  • Duplicate the query parameters in the component’s state so I know when they change
  • Or pass the query parameters down my view hierarchy from the router as a prop

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
abergscommented, Feb 5, 2015

@ryanflorence I believe you meant:

 componentWillReceiveProps (nextProps) {
    var old = this.props.query;
    var new = nextProps.query;
  }
1reaction
ryanflorencecommented, Feb 6, 2015

If I need that info I pass the params/query down my route view hierarchy and let react’s lifecycle hooks help me out.

Router.run(routes, (Handler, state) => {
  React.render(<Handler {...state}/>, document.body);
});

// and then everywhere else:
<RouteHandler {...this.props}/>

Now its available here on every component:

  componentWillReceiveProps (nextProps) {
    var oldQuery = this.props.query;
    var newQuery = nextProps.query;
  }

edit thanks @abergs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling a function in a component when a query parameter ...
It looks like react-router will call .render when a query parameter changes, but it's not clear how to tell which part of the...
Read more >
Is there a react component event for when url query string ...
Your component will get query params in this.props.location.search and when query params change componentWillReceiveProps will fire.
Read more >
Syncing query parameters with react state - Inkoop
We will be using it inside our react components and will call this function whenever we want to update our query parameters.
Read more >
Using React Hooks to sync your component state and the URL ...
First, we import the hooks useState and useCallback from React and the functions getQueryStringValue and setQueryStringValue we defined before ...
Read more >
Query Parameters - Routing - Ember Guides
Query parameters are optional key-value pairs that appear to the right of the ? in a URL. For example, the following URL has...
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