Trying on simple navigation with react-router-dom
. Managed to have it works with this.props.history.push('/pathName');
However, I have doubts about my understanding and implementation, appreciate your 2 cents on correcting my mistakes.
Am I in the right direction?
My use case scenario:
Firstly, I have 3 pages with a common component <Navbar>
implemented on top of each page. Navbar
consists of 2 buttons Page A and Page B. User should be navigated to either screen when the respective button is clicked.
- Homepage
- Page A
- Page B
My Implementation:
Homepage.js - (Besides the class name is different, both Page A and B has the same implementation)
import React from 'react';
import { Redirect } from "react-router-dom";
import Navbar from '../common/navbar';
class Homepage extends React.Component{
callBackFromNavBar = (value) => {
NavigationHelper.historyPush(this.props, value);
}
render(){
return (
<div>
<Navbar callback={this.callBackFromNavBar}/>
</div>
);
}
}
export default Homepage;
NavigationHelper.js
export const historyPush = (props,value) => {
console.log('helper calling');
switch(value){
case 'PageA':
props.history.push('/PageA');
break;
case 'PageB':
props.history.push('/PageB');
break;
default:
break;
}
}
Navbar - Following shows how the value is being pass back to parent
<Button variant="contained" onClick={ () => {
props.callback('PageA');
} }>Page A</Button>
Learning source:
Link to Sandbox