0

I have this folder structure

src
|
+-- pages
|     |
|     +--Dashboard.js
|     +--Homepage.js
|
+-- index.js

So index.js renders the homepage to the screen. On homepage.js I have a button, and I just want it so when the user clicks the button, it goes to dashboard.js

Here is index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Homepage from './pages/Homepage';
import registerServiceWorker from './registerServiceWorker';
import { BrowserRouter } from 'react-router-dom'

ReactDOM.render((
    <BrowserRouter>
        <Homepage />
    </BrowserRouter>), document.getElementById('root'));
registerServiceWorker();

And here is the Homepage with the button on it

import React, { Component } from 'react';


class Homepage extends Component {
  render() {
    return (
        <div>
            <h1>Homepage</h1>
            <button>Go to Dashboard</button>
        </div>
    );
  }
}

export default Homepage;

How can I achieve this? I can't just find a simple solution online. Thanks for help.

  • [Router](https://reacttraining.com/react-router/web/example/basic), all you need to do is call the components. And of course import the pages from where the component are coming. Let me know if you are confused. – Dhaval Jardosh Oct 13 '17 at 12:44
  • Check the duplicate for programatically navigating, However, You can just make the button as a link and route as usual `import {Link} from 'react-router-dom'` and then use it like `Go to Dashboard`. However for all this to work you first need to configure your Routes properly ` ` – Shubham Khatri Oct 13 '17 at 12:58
  • You can also use `browserHistory` from the `react-router` to push user to the new page. `import { browserHistory } from 'react-router'` and in your code `` What whatever @ShubhamKhatri says, makes sense as well. – Yoshi Oct 13 '17 at 13:25
  • You can also change `window.location.href` to the target URL. The browser will automatically redirect it. – buoyantair Oct 13 '17 at 13:35
  • @ShubhamKhatri Where do I need to write this code? I have trued for ages and can't figure it out –  Oct 13 '17 at 13:50
  • I think in my comment I have pretty much mentioned everything, if you read the comment properly should should be able to figure out where to write what code – Shubham Khatri Oct 13 '17 at 13:51

0 Answers0