I\'m trying to set up react-router in an example application, and I\'m getting the following error:
You should not use outside a
I kinda come up with this code :
import React from 'react';
import { render } from 'react-dom';
// import componentns
import Main from './components/Main';
import PhotoGrid from './components/PhotoGrid';
import Single from './components/Single';
// import react router
import { Router, Route, IndexRoute, BrowserRouter, browserHistory} from 'react-router-dom'
class MainComponent extends React.Component {
render() {
return (
<div>
<BrowserRouter history={browserHistory}>
<Route path="/" component={Main} >
<IndexRoute component={PhotoGrid}></IndexRoute>
<Route path="/view/:postId" component={Single}></Route>
</Route>
</BrowserRouter>
</div>
);
}
}
render(<MainComponent />, document.getElementById('root'));
I think the error was because you were rendering the Main component, and the Main component didn't know anything about Router, so you have to render its father component.
If you don't want to change much, use below code inside onClick()method.
this.props.history.push('/');
Write router in place of Main in render (last line in the code). Like this ReactDOM.render(router, document.getElementById('root'));
You can put the Link component inside the Router componet. Something like this:
<Router>
<Route path='/complete-profiles' component={Profiles} />
<Link to='/complete-profiles'>
<div>Completed Profiles</div>
</Link>
</Router>