2017-07-03 31 views
3

私が反応し-ルータ-DOMでこのパスを持っている:ボタンで経路をナビゲートする方法反応ルータv4をクリックしますか?

<BrowserRouter> 
    <div> 
    <Route exact path='/(index.html)?' component={Home}/> {/* app = home */} 
    <Route path='/register' component={Registration}/> 
    </div> 
</BrowserRouter> 

すべてが正常に動作している、今、どこにでも私のコンポーネントで、私はこのように、のonClickにより、コードをパスを変更したい:

<button onClick={() => history.push('/the/path') }> change path </button> 

どうやってやるの?

答えて

2
import {withRouter} from 'react-router-dom'; 
... 

class App extends React.Component { 
    ... 

    nextPath(path) { 
    this.props.history.push(path); 
    } 

    render() { 
    return (
     <button onClick={() => this.nextPath('/the/path') }> 
     change path 
     </button> 
    ); 
    } 
} 

export default withRouter(App); 
+0

ここで 'withRouter'とは何ですか? –

+0

@PardeepJain https://reacttraining.com/react-router/web/api/withRouter – soywod

関連する問題