1

I know this issue has to do with a fundamental misunderstanding of how React Router works, and more specifically, probably the history object. I have a component called Search, which lets the user search for a particular city. This component appears in multiple places throughout the app, including '/' and '/:cityname'.

From '/', the component works as expected, and correctly pushes the new url param onto the url and the url becomes '/vancouver'. However, from '/vancouver', when I use this same component, the url does not behave as expected. For instance if I enter Istanbul, I am correctly directed to /istanbul, but then as I proceed through the app and click on items, I expect to be directed to '/istanbul/item1'. However, what happens currently is that I end up at '/istanbul/istanbul/item1', which of course is not found, and returns a 404.

Here is the function that gets called when a city is selected (found within Search component)

    const onSuggestionSelected = (event, { suggestion }) => {
        props.history.push(`/${suggestion.nickname}`)
    }

App.js with routes

 <Switch>
            <Route exact path="/" component={HomePage} />
            <Route exact path="/terms-of-service" component={TermsOfServicePage} />
            <Route exact path="/privacy-policy" component={PrivacyPolicyPage} />
            <Route exact path="/:cityname/personalize" component={FilterPage} />
            <Route exact path="/:cityname/experiences" component={SearchPage}>
              <Redirect to="/:cityname" />
            </Route>
            <Route exact path="/:cityname" component={SearchPage} />
            <Route exact path="/:cityname/experiences/:experiencename" component={ExperiencePage} />
            <Route exact path="/:cityname/experiences/:experiencename/summary" component={(routeProps) => <SummaryPage {...routeProps} />} />
            <Route exact path="/:cityname/experiences/:experiencename/payment" component={PaymentPage} />
            <Route exact path="/:cityname/experiences/:experiencename/payment-successful" component={PaymentSuccessfulPage} />
            <Route component={NotFoundPage} />
            <GlobalStyle />
  </Switch>

ExploreMore Button

                <ButtonWrapper onClick={sendAnalyticsData}>
                    <LetsGoButton to={{
                        pathname: `${props.match.params.cityname}/experiences/${experience.nickname}`,
                        state: props.location.state
                    }}
                    palette="tertiary">
                        Explore more
                    </LetsGoButton>
                </ButtonWrapper>

Please let me know if there is anything else that I can provide that would be helpful. I've tried to do research on how history.push works exactly, but I haven't been able to find much. My best guess is that it takes the current location, and adds on the provided url. Even if that's the case, I can't understand why it would be applying istanbul twice.

enter image description here

8
  • afaik, by now react router does not support relative urls intentionally. So it's rather unexpected behavior. What's the version do you use? Commented Sep 30, 2019 at 19:22
  • using react router v4. The thing is, I'm trying to use absolute URLs. as can be seen, the absolute url being requested is /${suggestion.nickname}/ (which turns out to be /istanbul, /vancouver etc) Commented Sep 30, 2019 at 19:32
  • Can you share your main routing code or start point? Commented Sep 30, 2019 at 19:41
  • @Garry thank you. updated Commented Sep 30, 2019 at 20:00
  • Any reason for using "<Redirect to="/:cityname" />" as you already have defined the route for it. Commented Sep 30, 2019 at 20:45

1 Answer 1

6

I figured out the problem on this one. One of the commenters suggested that I had been using relative paths rather than absolute. I erroneously thought that he was incorrect, seeing as I seemingly have the full url in there. My mistake was to not start off the url with /

before: ${props.match.params.cityname}/experiences/${experience.nickname}

after: /${props.match.params.cityname}/experiences/${experience.nickname}

I hope this helps someone out.

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

1 Comment

I was about waste my life but this saved my precious minutes.

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.