OneCompiler

How to get the Query params in React

311


How to get the query params from a URL in React

In this post let's see how to get the current query params inside a react component

Getting the query Params using React Router

consider you have a URL as below

http://localhost:3000/posts/categories?name=react

To get the query param in above URL we use location Prop

import React from 'react'

const Posts = ({location}) => {
	const search = location.search;
	const name = new URLSearchParams(search).get('name');
	return (
	<h1>{name}</h1>
	)
}

That's it Guys give it a 👍 if you found it useful.