2

index.js - file

<AppProvider>
    <BrowserRouter>
        <div>
          <Route exact path="/" component={Home} />
          <Route exact path="/balances" component={Balances} />
        </div>
    </BrowserRouter>
</AppProvider>

On Home Component, I have a button and its event click was send to App Provider successfully.

   <button className="btn btn-primary">{context.Process_ToBalances}</button>

And on AppProvider, I have a method like this.

Process_ToBalances()
{
   //pre process data
   // Redirect to Balance page here
}

ButI have no idea why all the redirection method failed.

I tried both of browserHistory.push('/Balances') and this.props.push("/Balances") but still failed.

RIYAJ KHAN
  • 13,830
  • 5
  • 27
  • 48
Hai Tran
  • 107
  • 1
  • 10

1 Answers1

2

In order to use react-router functionality in AppProvider, your Router must wrap you AppProvider component and you AppProvider must receive the Router Props, you can do so like

<BrowserRouter>
    <AppProvider>
        <div>
          <Route exact path="/" component={Home} />
          <Route exact path="/balances" component={Balances} />
        </div>
    </AppProvider>
</BrowserRouter>

and then in AppProvider use withRouter

export default withRouter(AppProvider);

After this you can use this.props.history.push('/balances), note the case sensitive pathname /balances and not /Balances

Refer this question for more details on How to Programmatically navigate using react-router

Shubham Khatri
  • 211,155
  • 45
  • 305
  • 318
  • When I read your comment, I found my mistake. Just move the AppProvider inside BrowserRouter and it can use BrowserRouter props. Thank you, you made my day – Hai Tran Jun 29 '18 at 06:31