2

I have multiple data & I want to display data as per query string, how can I do that in reactJs?

My code:

const Ladies = () => {

  return (
      <div>
      if('http://localhost:3000/ladies?service=makeup'){
      <div>Makeup Content</div>
      }
      else('http://localhost:3000/ladies?service=hairStyling'){
      <div>Hair Styling Content</div>
      }      
      </div>
      )
      
}

Thank You!

1
  • You can use const getQueryStringValue = (queryString) => { new URLSearchParams(window.location.search) return urlParams.get(queryString); } function to find out query string value and then generate the divs Commented Jun 16, 2021 at 15:30

1 Answer 1

3

I consider this for your url

http://localhost:3000/ladies?service=makeup

In your code

const params = new URLSearchParams(window.location.search)

check if it has the query

params.has('service')?params.get('service'):""

or log it

console.log(params.has('service')?params.get('service'):"")

return will be makeup

I'm not sure but i think it will be string so if you want to use it check if it's equal to "makeup" like so

<div> {params.has('service')&&params.get('service')==="makeup"?"There is makeup":"There is no make up in query strings !"}</div>

Update:

you can put that just like a text to show a div instead, that is a great feature of jsx, do it like this.

<div>
  {params.has("service") && params.get("service") === "makeup" ? (
    <div>Makeup section</div>
  ) : params.get("service") === "hairStyling" ? (
    <div>hair styling section</div>
  ) : (
    <div>cannot find any query strings</div>
  )}
</div>

For more info check out here

Sign up to request clarification or add additional context in comments.

3 Comments

Its working fine but there would be multiple condition how can i manage that?
I have to display DIV SECTION in conditions instead of just text...
Yahya parvar Thank you so much this is working fine now...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.