On top of my app, I have the following main router
export class App extends React.Component {
render() {
return (
<div>
<Router>
<Switch>
<Route exact path={Paths.login} component={LoginForm} />
<Route path={Paths.dashboard} component={Dashboard} />
</Switch>
</Router>
</div>
)
}
}
Further in dashboard, I have a side menu with links
export class Dashboard extends React.Component<RouteComponentProps> {
constructor(props: RouteComponentProps) {
super(props)
this.handleLogoutClick = this.handleLogoutClick.bind(this)
}
handleLogoutClick() {
UserRepository.logout()
this.props.history.push(Paths.login)
}
render() {
return UserRepository.isLoggedIn() ? (
<div>
<BrowserRouter>
<div id="menu">
<div className="title">Dashbaord</div>
<Nav className="flex-column" defaultActiveKey>
<Link to={`${this.props.match.path}/users`}>Users</Link>
<Nav.Link onClick={this.handleLogoutClick}>Logout</Nav.Link>
</Nav>
</div>
<div id="content">
<Switch>
<Route path={`${this.props.match.path}/users`} component={Users} />
</Switch>
</div>
</BrowserRouter>
</div>
) : Paths.redirectToLogin()
}
}
When I go to dashbaord and click users link, it works, but if I refresh the page with same url, it shows blank page. Any solution on how I can go directly to child route?
I am very new to react js, kind a learning now. I found debugging this routing thing very difficult, you don't get any error or clue what went wrong!
webpack config file
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const distDir = path.resolve(__dirname, 'dist')
module.exports = {
entry: [
"./src/index.tsx"
],
mode: 'development',
output: {
filename: "main.js",
path: distDir
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'babel-loader',
exclude: /node_module/
},
{
test: /\.scss$/,
use: [
'style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
plugins: [
new HtmlWebpackPlugin(
{
title: 'Dashboard',
template: 'index.html'
}
),
new MiniCssExtractPlugin()
],
devServer: {
contentBase: distDir,
historyApiFallback: {
index: 'index.html'
},
hot: true
},
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
}