1

I need to customize the style of my React.js app based on which client is using it, and I'm trying to pass the client id in the url. The url looks like this:

/#/app/client/dashboard

I created the Rout using this param:

<Route path="/app/:client/dashboard" component={Dashboard} />

Great. Now I want to retrieve the param in my root component. I tried two things. The first is to see if it's in the props object:

export default function App(props)

but it's not there.

Then I tried to set the localStorage with a client item in the Dashboard component, but that's too late because the App() function is called before the Dashboard() function.

Is there any other way to do this?

5
  • There isn't a params match in props of Dashboard component? Commented Apr 14, 2020 at 7:33
  • if I get it straight you get the clientId in Dashboard component but you want to use it in the parent component? you want the parent component to also be able to read the query param Commented Apr 14, 2020 at 7:35
  • @naortor yes, exactly. I want it in the parent component. Commented Apr 14, 2020 at 7:38
  • @Irrech no, just __proto__ and there's no match in it. Commented Apr 14, 2020 at 7:39
  • It can only be passed to a child component of the rendering Route. Where is App component in relation to Dashboard? Commented Apr 14, 2020 at 8:13

1 Answer 1

1

I'm not sure whether this is a right approach. If just the problem needs to be solved, give a try with window.location, as it will give access to the current url

let url = window.location.pathname;
console.log(url);

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

2 Comments

This is not an answer but a comment please visit and check how to answer a question.
Thanks, the pathname is empty, but window.location.href has the information.

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.