React Router: Query Param Match? - reactjs

According to the accepted answer to this question, React Router 4 doesn't match query parameters anymore. If I go from a URL matched by one of my <Route>s to the same URL with a different query string, the content doesn't seem to change. I believe this is because navigating between URLs that match the same <Route> doesn't change the content, but please correct me if I'm wrong. Given this, how do I use React Router for a set of URL's that need to differ only by query parameter?
For example, many search engines and other sites that use search bars, including the site I am working on, use a query parameter, commonly q or query. The user may search for one thing, then decide that is not what he/she wants and search for another thing. The user may type in the second URL or search with the search bar again. There isn't really a place for the search term in the URL path, so it kind of needs to go in the query string. How do we handle this situation?
Is there a way, with React Router, to link to a URL that only differs in the query string and change the content, without refreshing the entire page? Preferably, this wouldn't require any external library besides React and React Router.

Try the render function prop instead of component prop of Route. Something like this:
<Route render={props => {
// look for some param in the query string...
const useComponentA = queryStringContains('A');
if(useComponentA) {
return <ComponentA {...props}/>;
} else {
return <ComponentB {...props}/>;
}
}}/>

There are 2 ways to do that:
1) Use location.search in react component to get the query string, then pass it to child component to prevent re-rendering the whole component. React-router has the official example about this.
2) Define a regex path of router to catch the query string, then pass it to react component. Take pagination as an example:
routes.js, for router config you can refer this
const routerConfig = [
{
path: '/foo',
component: 'Foo',
},
{
path: '/student/listing:pageNumber(\\?page=.*)?',
component: 'Student'
},
Student.js
render() {
// get the page number from react router's match params
let currentPageNumber = 1;
// Defensive checking, if the query param is missing, use default number.
if (this.props.match.params.pageNumber) {
// the match param will return the whole query string,
// so we can get the number from the string before using it.
currentPageNumber = this.props.match.params.pageNumber.split('?page=').pop();
}
return <div>
student listing content ...
<Pagination pageNumber = {currentPageNumber}>
</div>
}
Pagination.js
render() {
return <div> current page number is {this.props.pageNumber} </div>
}
The 2nd solution is longer but more flexible. One of the use cases is server sider rendering:
Apart from the react components, the rest of the application (e.g. preloaded saga) need to know the url including query string to make API call.

Related

How do I replace queries in the URL when on the same path? | Without useHistory() if possible

I have a page that displays products returned from my API based on the URL's search parameters. For example, '/products?search=socks' would return products with the word socks in the title. The search bar is located in the header. If I search for a product on the home page (a different path: '/'), it navigates to the product list page and displays the products. However, if I search for a product on the product list page (the same path: '/products'), it re-renders and removes the search parameters, leading to no products being displayed. I prefer to use current hooks as it appears useHistory is outdated.
I have tried to navigate to the URL directly:
navigate(`/products?search=${search}`);
And I have tried to set the parameters as one source suggested:
const params = new URLSearchParams();
params.set('search', search);
navigate(`/products?search=${search}`);
I expected the query/search parameter to replace the search value and re-render with the new results.
I would use the new React Router V6 useSearchParams hook:
import { useSearchParams } from 'react-router-dom'
Then instantiate the hook at the top of your component:
let [searchParams, setSearchParams] = useSearchParams();
Then you can manipulate the search params anywhere, in your case:
setSearchParams({ search: '...' );
Doing this along-side the navigate() call will basicallly lead the user to the page as the search param is being changed.
In your situation, I would detect if the user is already on the product list page before always running navigate(). You can use the useLocation() hook to detect what pathname you currently are on.
If you're on the product list page already, don't run navigate(). Your useEffect would be responsible for re-rendering the page when search params change using a dependency array [searchParams.search]:
useEffect(() => {
// do stuff when search params "search" variable changes
}, [searchParams.search])

Changing Query paramers while staying on the same page without reload= NextJS Router

for a project I am working on I am running into a problem with the nextjs Router.I have a component that has an input field which the user should be able to input their searchterm in. There is a different component which should be able to get this searchterm and perform a search.
Because the two components aren't connected I would like to set the queryParameters in the router in the Input component, and then execute a function in the search component when the searchTerm is changed.
The problem lies in the following: The searchComponent receives the nextJS router as props and will only execute my useEffect function when those props are changed (and react knows they are changed), on top of that I need to stay on the same page when updating the query parameters, but the route of this page is dynamic. For example: the user can add this combination of components on /search but also on /lookforitem.
I have tried setting the queryParameters in the following way in the Input component:
function setQueryParams() {
router.query = {
...router.query,
searchTerm: input.current,
};
}
In combination with the following code in the Search component:
useEffect(() => {
console.log('Router has changed');
}, [router]);
The problem is that this useEffect doesnt get called untill the search component is rendered again (I have created a button that logs the router to the console, and it shows the updated router), which I assume is because React hasn't realised that the Router props have changed.
I have also tried setting the query parameters via a router.push in the following way:
function setQueryParams() {
router.push(
{
pathname: router.route,
query: {
...router.query,
searchTerm: input.current,
},
},
undefined,
{ shallow: true }
);
}
However this comes with its own set of problems. First of all it causes a refresh of the page, which I don't want. On top of that it changes the url to for example: /search?searchTerm=Hello which means that if I enter a different input and submit it will stack making the next url for example: &searchterm=hello?searchterm=goodbye.
I want a way to update the query parameters without refreshing the page, but while also notifying the other components that use the router that the query parameters have updated. All of the searching that I've done seems to be specific to either routing to a different page or routing to a predefined page.
Any help would be greatly appreciated.

Incorrect routes appearing valid

I am using path variables in my Route components. Initially, I had
<Route path={"/:brand/"} component={HomeContainer} />
but I had to change path attribute to
path={"/:brand(brandOne|brandTwo)/"}
because incorrect URLs appeared to be valid. The code above works for my 2 brand sections.
How would you suggest I do this for my products container as I have many products and I don't want random incorrect URLs to show an empty page with SiteHeader and SiteFooter.
Thanks
If you have many params that you want to validate for, you can simply store the valid ones in an array and check for the param while rendering, If the param is not among those, redirect to either 404 or home page
render() {
const { match} = this.props;
if(!validParams.includes(match.params.brand)) {
return <Redirect to="/404" />
}
// return normal component here
}

How to get option param value from react router without follow sequence (React Router v4)

I have to access value to optional param(s) form a react router (React Router v4). Now problem is as we have avoid param(s) sequence.
For example as per following react router code we have to access pathPraram2 without define pathPram1 :
<Route path="/to/page/:pathParam1?/:pathParam2?" component={MyPage} />
You can use withRouter hoc from the react-router-dom (or from native router).
This will pass some routing props to your component.
Validate it with flow if you want to:
type PropType = {
match: {
params: {
pathParam1?: string, // '?' to emphasize its optional
pathParam2?: string,
},
},
};
In your component you can use them by simply checking for their existence.
if (this.props.params && this.props.params.pathParam1) {
// do something
}
I hope this helps.
withRouter: https://reacttraining.com/react-router/native/api/withRouter
Also, extending on #Shubham's answer, I recommend having these optional params with identifiers.
Example:
http://example.com/page/identifier-1/param-1/identifier-2/param-2
http://example.com/page/identifier-2/param-2
http://example.com/page
This way you can define a route based on page, and in the component you can check based on identifier the param your route contains.
For a route with params like such:
{
articleId: 'abc',
commentId: 'xyz'
}
To create a route mentioned above:
<Route path="/page/:key1?/:value1?/:key2?/:value2?" component={MyPage} />
Now to access these in component:
if (!this.props.match || !this.props.match.params) {
return;
}
let articleId = null;
let commentId = null;
if (
this.props.match.params.key1 &&
this.props.match.params.value1
) {
if (this.props.match.params.key1 === 'articleId') {
articleId = this.props.match.params.value1;
}
if (this.props.match.params.key1 === 'commentId') {
commentId = this.props.match.params.value1;
}
}
if (
this.props.match.params.key2 &&
this.props.match.params.value2
) {
if (this.props.match.params.key2 === 'articleId') {
articleId = this.props.match.params.value2;
}
if (this.props.match.params.key2 === 'commentId') {
commentId = this.props.match.params.value2;
}
}
then just simple check if articleId or commentId is null or not.
I think this will lead to a better url formation, though it increases conditions inside the component to get the parameters.
However in Shubham's answer, you will have less conditions and a different url formation. It's upon you to decide which one is preferable for you to use as per your use case.
Hope it helps.
It isn't directly possible for the Router to have an optional path param directly before the other
Consider the case
<Route path="/to/page/:pathParam1?/:pathParam2?" component={MyPage} />
Now say you wish to have pathParam1 and not pathParam2, so your route path will be
/to/page/pathpage1
Now consider another case when the pathParam1 is not passed but pathParam2 is, so route path looks like
/to/page//pathpage2
Now here pathParam1 will match as empty whereas the pathParam2 will match to pathpage2 which will work and you can access it like this.props.match.pathparam2
however it not clean in terms of the url as it contains two //. As better way would be to introduce a fixed path in between the params
<Route path="/to/page(/:pathParam1?)/somepath(/:pathParam2)?" component={MyPage} />

Check if react-router path is active

How to check if generic react-router path matches current location pathname?
react-router path: /Movies/:id
location.pathname: /Movies/56fa7446bae6eb301e5937f3
I want to use route paths with menu buttons, to set class="active".
EDIT:
To clarify, paths in my app look like:
/Movies/56fa7/watch
and not like:
/Movies/watch/56fa7
How do I check if the former route is active?
Is it doable without <Link> component?
/Movies/56fa7/watch is arbitrary after /Movies, and <Link> obviously can't be pointed to an arbitrary location. So let's ignore <Link> for a moment:
Is there a standalone function or property in react-router that checks if /Movies/:id/watch is active?
According to the docs, you could use matchPath function which takes two arguments:
pathname you want to match (String).
options (Object) or path (String) to match against.
If matched it will return an object of this shape:
{
path, // the path used to match
url, // the matched portion of the URL
isExact, // whether or not we matched exactly
params
}
Otherwise you'll get null.
To make use of it in your components you could simply do:
import { matchPath } from 'react-router';
// ...
render () {
const isMovieWatchPathActive = !!matchPath(
this.props.location.pathname,
'/Movies/:id/watch'
);
// ...
}
Hope it'll help someone.
As of React Router v4 (March 2017):
I'm a bit late to the party, but hopefully this will help anyone with the same question. When a component is rendered through a Route, certain props are passed to it. These props can be used to determine which route is active.
In a component rendered by a Route, you can use this.props.match.url to get the actual URL requested by the browser, and you can use this.props.match.path to get the path pattern for the current route.
Check working example here: https://codesandbox.io/s/20o7q0483j
Docs related to this are available here:
https://reacttraining.com/react-router/web/api/Route/Route-props
use this
import { matchPath } from "react-router";
const match = matchPath("/users/123", {
path: "/users/:id",
exact: true,
strict: false
});
Check out the Link's property: activeStyle or activeClassName. They are supposed to automatically set the link to active when route matches. See the example: https://github.com/reactjs/react-router-tutorial/tree/master/lessons/05-active-links

Resources