S
S
Sergey2020-08-02 14:44:57
React
Sergey, 2020-08-02 14:44:57

How to get history in this case?

Hello.
In a react app, I am using react-router-dom. The code below shows the structure (App.js):

<Layout>
        <Header/>
        <Switch>
          <Route path = '/' exact component = {Home}/>
          <Route path = '/news' component = {News}/>
          <Route path = '/profile' component = {Profile}/>
          <Route path = '/login' component = {Login}/>
        </Switch>
      </Layout>


In each component, I can get the history to redirect via props. But how to get history in this component (App.js)?
index.js code
ReactDOM.render(
  <Router history = {history}>
        <App />
  </Router>,
  document.getElementById('root')
);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question