Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router V4 Browser Back not working

I've built an app using react router v4 which isn't working as expected. The browser back button doesn't work at all. I thought it was due to some leftover state, but doesn't seem to be the case. Here is the render method:

So routing works as expected but when you click back nothing happens. Am I missing something?

<div id="redeem-root">
  <BrowserRouter basename={url.basename()}>
    <Fragment>
      <Route
        exact
        path={url.redeemHome()}
        render={() =>
          goToConfirmationPage ? (
            <Redirect to={url.confirm()} push={true} />
          ) : (
            <EnterGiftCard
              loading={this.state.loading}
              selectedCardType={this.state.selectedCardType}
              giftCardStartRedemption={this.giftCardStartRedemption}
              isSmallView={this.state.isSmallView}
              error={this.state.error}
              setActiveCardType={this.setActiveCardType}
              setPin={this.setPin}
              pin={this.state.pin}
            />
          )
        }
      />
      <Route
        exact
        path={url.confirm()}
        render={() =>
          goToThankYouPage ? (
            <Redirect to={url.thankYou()} push={true} />
          ) : (
            <ConfirmWithClickTracking
              loading={this.state.loading}
              pin={this.state.pin}
              selectedCardType={this.state.selectedCardType}
              entitlement={this.state.entitlement}
              giftCardConfirmRedemption={this.giftCardConfirmRedemption}
              error={this.state.error}
              trackingInfo={{ pageName: 'RedeemConfirm' }}
            />
          )
        }
      />

      <Route
        exact
        path={url.thankYou()}
        render={() => (
          <ThankYouWithClickTracking
            selectedCardType={this.state.selectedCardType}
            userEmailAddress={this.state.userEmailAddress}
            entitlement={this.state.entitlement}
            resetState={this.resetState}
            trackingInfo={{ pageName: 'RedeemThankYou' }}
          />
        )}
      />
    </Fragment>
  </BrowserRouter>
</div>
like image 652
Sean Avatar asked Dec 30 '25 23:12

Sean


1 Answers

It seems like doing push={true} in your Redirect component is pushing an extra stack into your BrowserHistory. Remove that and it should work

By the way, you don't need to specify push={true}. Since it's a boolean props you can just do <Redirect to={url.thankYou()} push/>

like image 180
Tan Kim Loong Avatar answered Jan 01 '26 11:01

Tan Kim Loong