Search code examples
javascriptreactjswebpackbabeljseslint-config-airbnb

react-router-dom isnt working just render "/"


im having some throubes trying to render multiples pages en react. im new in react thats why i was loooking in many pages and tutorials.Im using react, webpack, babel and eslint airbnb

when i render my react app look like this.

hows render homepage

the page App that i use to render all routes is like this.

import React from 'react';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
import Homepage from './Homepage';
import Layout from '../components/Layout';
import NotFound from './NotFound';
// import NotFound from './NotFound';

function App() {
  return (
    <BrowserRouter>
      <Layout>
        <Switch>
          <Route exact path='/' component={Homepage} />
          <Route exact path='/homepage' component={Homepage} />
          <Route path='/404' component={NotFound} />
          <Redirect from='*' to='/404' />
        </Switch>
      </Layout>
    </BrowserRouter>
  );
}

export default App;

then when i go to the route /homepage or a whatever route look like this

render nothing

NotFound page code is like this.

import React from 'react';

function NotFound() {
  return <h1> Error 404: Not Found</h1>;
}

export default NotFound;

my Layout is like this

import React from 'react';
import Header from './Header';
import Footer from './Footer';

function Layout(props) {
  const { children } = props;
  return (
    <>
      <Header />
      {children}
      <Footer />
    </>
  );
};

export default Layout;

i was looking answers to solve it, and maybe could be the webpack config that im using but im not sure. my webpack looks like this.

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  // para definir nuestra entrada index.js y la salida nuestro archivo bunble.js
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  //  para resolver nuestros elementos
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.html$/,
        use: {
          loader: 'html-loader',
        },
      },
      {
        test: /\.(s*)css$/,
        use: [
          { loader: MiniCssExtractPlugin.loader },
          'css-loader',
          'sass-loader',
        ],
      },
      {
        test: /\.(png|gif|jpg)$/,
        use: [
          {
            loader: 'file-loader',
            options: { name: 'assets/[hash].[ext]' },
          },
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      filename: './index.html',
    }),
    new MiniCssExtractPlugin({
      filename: 'assets/[name].css',
    }),
  ],
};

my package.json is like this

{
  "name": "legendary-deca-meca-app",
  "version": "1.0.0",
  "description": "Legendary app ",
  "main": "index.js",
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^3.4.1"
  },
  "devDependencies": {
    "@babel/core": "^7.10.2",
    "@babel/preset-env": "^7.10.2",
    "@babel/preset-react": "^7.10.1",
    "babel-eslint": "^10.1.0",
    "babel-loader": "^8.1.0",
    "css-loader": "^3.5.3",
    "eslint": "^7.2.0",
    "eslint-config-airbnb": "^18.1.0",
    "eslint-plugin-import": "^2.21.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.20.0",
    "file-loader": "^6.0.0",
    "html-loader": "^1.1.0",
    "html-webpack-plugin": "^4.3.0",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.14.1",
    "sass-loader": "^8.0.2",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --open --mode development"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/RaPzoD1/legendary-deca-meca-app.git"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/RaPzoD1/legendary-deca-meca-app/issues"
  },
  "homepage": "https://github.com/RaPzoD1/legendary-deca-meca-app#readme"
}

thanks for your comments and your time.


Solution

  • Guys thanks for your help, i found the answer. the problem was react-router-dom, im using "react-router-dom": "^5.2.0", but the tutorial that i saw was react-router-dom version 4.3.1

    i made the next changes

    import React from 'react';
    import { HashRouter as Router, BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';  ---> i import HashRouter as Router
    import Homepage from './Homepage';
    // import Layout from '../components/Layout';
    import NotFound from './NotFound';
    import Header from '../components/Header';
    import Footer from '../components/Footer';
    // import NotFound from './NotFound';
    
    function App() {
      return (
        <Router> -----> here i change the BrowserRoter for Router
          <Header /> --> here i erase the layout and i put teh <header> and <footer>
          <Switch>
            <Route exact path='/' component={Homepage} />
            <Route exact path='/homepage' component={Homepage} />
            <Route path='/404' component={NotFound} />
            <Redirect from='*' to='/404' />
          </Switch>
          <Footer />
        </Router>
      );
    }
    
    export default App;
    

    here are the screenshots homepage NotFound