2

Lets say I have a serverless website. MyWebSite.com

If a user types MyWebSite.com/randomString

How can I capture this extra random information.

The Goal is to Dynamically Display Content based on users Random Url Search

Investigating This Question

Read the current full URL with React?

2
  • you want like youtube search right? Commented Nov 26, 2019 at 5:26
  • Sure basically I want to setState a variable based on that randomString Commented Nov 26, 2019 at 5:29

2 Answers 2

1

Try to use react-router-dom instead of react-router

Ok hopefully your route is like <Route path="/:id" component={Search} />, in this case you can get the randomString like follows

in functional component:

props.match.params.id

or in class component:

this.props.match.params.id
Sign up to request clarification or add additional context in comments.

1 Comment

I'm going to try this, instead of the answer I posted, because with my method App component has to mount first and then you have to change a state variable and then you have to call on a component to mount. Seems like with react-router-dom the component renders based on the URL path automatically.
1

After Reading the previous question I found this to works just fine.

 componentDidMount() {
        console.log(window.location.href);

      }

Comments

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.