0

I want to trigger a function whenever the url contains a specific keyword. Any idea on how this can be done?

NOTE : It can be done separately for each route, but that would be a tedious solution.

1
  • You can track the url in the parent state and pass it to a child prop which could be a HOC. The handle logic would be in that single child of the parent. Commented Jun 11, 2018 at 19:15

1 Answer 1

2
  1. You basically want to listen for @@router/LOCATION_CHANGE action type to be dispatched, or whichever the action type is for your version of react-router.

  2. Then use a regex or some kind of matcher.

    const url = www.google.com/routeParam
    const newRegex = new RegExp(/routeParam/, "gi");
    const hasKeyWord = url.match(newRegex);
    if (hasKeyWord) {
        console.log("// ... we can do function here")
    }
    
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.