0

this is my route:

<Route path="/about-cards/:id?" component={AboutCards} />

then this is my url:

http://localhost:3000/about-cards?id=1

inside my AboutCards component:

  componentDidMount() {
    console.log('this.props.match.params.id',this.props.match.params.id)

but it print a undefined

1 Answer 1

1

this.props.match.params will gives only url params and not query params.

Use this.props.location.search.It will gives you ?id=1. Do some processing to get id.

In modern browsers that support URL API,it can be achived using URLSearchParams.

let queryParams = new URLSearchParams(this.props.location.search);

queryParams.get('id'); //instead id,one can query param name.
// will give 1
Sign up to request clarification or add additional context in comments.

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.