react router dom can't get page - javascript

I'm using react with express using web pack but react router dom create an error i.e The null match problem.It can't get the page and the match is Null.
MY routes page code is: routes.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route} from 'react-router-dom';
import App from './App.js';
import language from './language.js';
import Page from './Page.js';
const Routes = () => (
<Router>
<div>
<Route path='/' component={App} />
<Route path='/page' component={Page} />
<Route path='/language' component={language} />
</div>
</Router>
)
console.log(typeof Routes)
export default Routes;

I have created sample snippet same as your code.
But its working fine. I am not getting any error
Check out this Snippet

Related

React-Router-Dom (v6) not working / doesn't render component

When I try to render the "Home" component it just doesn't show anything... The path is correct, and the Router imports should be fine too. What am I missing? It worked on another project of mine and even if I copy and paste that code it doesn't seem to work.
App.js:
import React from "react";
import Home from "./components/Home"
import {BrowserRouter as Router, Route, Routes} from "react-router-dom"
function App() {
return (
<Router>
<Routes>
<Route exact path="/" element={<Home/>}/>
</Routes>
</Router>
);
}
export default App;
Home.js:
import React from 'react'
function Home() {
return (
<div>Home</div>
)
}
export default Home
index.js:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
I expected the "Home" component to be rendered as soon as I load the page, but it wasn't (Blank page). I checked if I imported it incorrectly, but as soon as I delete the "Router" and "Routes" tags as well as the Route itself and just try to render the "Home" component on its own, it works.
It's probably something really small that I'm missing but I can't seem to figure it out right now.
Using Route requires Switch component which means you'll do the following:
import React from "react";
import Home from "./components/Home"
import {BrowserRouter as Router, Route, Routes, Switch} from "react-router-dom"
function App() {
return (
<Switch>
<Route exact path="/" >
<Home/>
</Route>
</Switch>
);
}
export default App;

React routes not rendering [duplicate]

This question already has answers here:
React Router Dom routes are returning blank pages
(2 answers)
Closed 8 months ago.
I'm following along with this React Tutorial on YouTube but I can't get my routes to render on own dev server.
This is what my home screen is rendering. My home screen should show a pink background underneath the navigation bar.
I suspect the error is happening in this section of my App.js code, since everything else outside of works fine:
<Routes>
<Route exact path="/" component={HomeScreen}/>
</Routes>
I'm using react-router-dom v6.3.0
My App.js code:
import './App.css';
import { useState } from "react";
import { BrowserRouter as Router, Routes, Route} from "react-router-dom";
// Screens
import HomeScreen from './screens/HomeScreen';
// Components
import Navbar from './components/Navbar';
import SideDrawer from './components/SideDrawer';
import Backdrop from './components/Backdrop';
function App() {
const [sideToggle, setSideToggle] = useState(false);
return (
<Router>
<Navbar click={() => setSideToggle(true)} />
<SideDrawer show={sideToggle} click={() => setSideToggle(false)} />
<Backdrop show={sideToggle} click={() => setSideToggle(false)} />
<main>
<div className="app">This is a test</div>
<Routes>
<Route exact path="/" component={HomeScreen}/>
</Routes>
</main>
</Router>
);
}
export default App;
My index.js code:
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
My HomeScreen code:
import "./HomeScreen.css";
const HomeScreen = () => {
return (
<div className="homescreen">
Home Screen
</div>
)
}
export default HomeScreen
Hope someone's able to give some advice to a React newbie?
Please let me know if I need to provide more info.
Wen, If you are using react-router-dom v6.3.0. then you have to use
<Routes>
<Route exact path="/" element={<HomeScreen/>}/>
</Routes>
I think there is your error:
import React from "react"; import { BrowserRouter} from "react-router-dom"; import ReactDOM from "react-dom";import "./index.css";import App from "./App";import reportWebVitals from "./reportWebVitals";ReactDOM.render(<React.StrictMode>BrowserRouter><App>BrowserRouter>React.StrictMode>,document.getElementById("root")reportWebVitals();
you need to cover app component in browserouter if it doesn't work than update your react-router-dom ande try thise simple docs
It should be element, not component, that property that renders a component for a Route, and the component should be called:
<Route exact path="/" component={<HomeScreen/>}/>
What you have there is for React Router Dom v5.

My react app doen't display any content after I deploy it on digital ocean

I've successfully deployed my react app on digital ocean as a static site. But when I click to see the live app the link only shows me a blank page, instead of show me the landing page (dashboard).
The command I used for the build
npm build
The code in my index.js file
import React from "react";
import ReactDOM from "react-dom";
import { createBrowserHistory } from "history";
import { Router, Route, Switch, Redirect } from "react-router-dom";
// core components
import Admin from "layouts/Admin.js";
import RTL from "layouts/RTL.js";
import "assets/css/material-dashboard-react.css?v=1.9.0";
import 'bootstrap/dist/css/bootstrap.min.css';
const hist = createBrowserHistory();
ReactDOM.render(
<Router history={hist}>
<Switch>
<Route path="/admin" component={Admin} />
<Route path="/rtl" component={RTL} />
<Redirect from="/" to="/admin/dashboard" />
</Switch>
</Router>,
document.getElementById("root")
);
my project structure
[1]: https://i.stack.imgur.com/JL2Yi.png
I found the solution. I just had to edit the package.json file and change the homepage attribute like this:
"homepage": ""

How to generate sitemap for pages generated dynamically in React.js

My backend is in django. I can get post data from there and also post list.
In reactjs, I am just displaying post details and lists (and some more details related to posts...)
In reactjs my index.js file is as below
import ReactDOM from "react-dom";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import React from "react";
import { BrowserRouter } from "react-router-dom";
import { GoogleReCaptchaProvider } from "react-google-recaptcha-v3";
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root"),
);
serviceWorker.unregister();
My app.js file is as below
import React from "react";
import { Route, Switch } from "react-router-dom";
import HomePage from "./pages/HomePage";
import BlogPage from "./pages/BlogPage";
import "./css/styles.css";
import CategoryPostsPage from "./pages/CategoryPostsPage";
import SearchPage from "./pages/SearchPage";
import ContactPage from "./pages/ContactPage";
function App() {
return (
<main>
<Switch>
<Route path="/post/:slug/" component={BlogPage} />
<Route
path="/category/:slug/"
component={CategoryPostsPage}
/>
<Route
path="/query/:query/"
component={SearchPage}
/>
<Route path="/contact/" component={ContactPage} />
<Route path="/" component={HomePage} />
</Switch>
</main>
);
}
export default App;
I add new posts from backend (django admin panel).
And through API, react app gets data from backend.
I want to have all urls like
/post/some-slug
/post/some-slug-2
...
/
/contact/
...
This is for SEO purpose, When I search, it displays only homepage url and contact page url, not a single url for any post...
Please suggest, what should I do,
I have tried sitemap packages from npm, but doesn't seem working...

Browser Router from React-Dom not working

I am attempting to serve a react app from the public folder of my rails app. I am building the js file and putting it in the public folder. When I go to the root of the app, I can see that the js and my index.html page have loaded. However, when I try to go to page, like /landing, I get a 404, route not found from Rails. I can't figure out why the react router is not kicking in. This all works on dev where I am serving the react app with a second server, I only get this issue in production. Any suggestions?
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.scss';
ReactDOM.render(<App />, document.getElementById('root'));
App.js
import React from 'react';
import Auth from './util/auth';
import { Redirect, BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import MyAccount from './components/my_account';
import MyListings from './components/my_listings';
import LoginPage from './components/login_page';
import LandingPage from './components/landing_page';
import RegistrationForm from './components/registration_form';
import PasswordResetForm from './components/password_reset_form';
import RequestPasswordResetForm from './components/request_password_reset_form';
import {FlashMessages} from './components/flash_messages';
import $ from 'jquery';
import popper from 'popper.js';
import './stylesheets/App.css';
window.Popper = popper;
window.jQuery = $;
window.$ = $;
global.jQuery = $;
require('bootstrap');
const App = appProps => (
<div>
<div id="flash-messages">
<FlashMessages />
</div>
<Router>
<div className="App">
<Switch>
<Route exact name="index" path="/landing" component={LandingPage} />
<Route exact name="login" path="/login" component={LoginPage} />
<Route exact name="register" path="/register" component={RegistrationForm} />
<Route exact name="reset_password" path="/reset_password" component={PasswordResetForm} />
<Route exact name="reset_password_request" path="/reset_password_request" component={RequestPasswordResetForm} />
<PrivateRoute path="/my_account" component={MyAccount}/>
<PrivateRoute path="/my_listings" component={MyListings}/>
</Switch>
</div>
</Router>
</div>
);
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
Auth.isAuthenticated() ? (
<Component {...props}/>
) : (
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}}/>
)
)}/>
)
export default App;
A typical gotcha with React Router is that you need to return the same index.html page for all routes - be it /, /landing-page/ or /a/really/deep/route/.
You typically solve that by adding a catch-all route. I don't know rails all that well, but I think this answer might help you out.
The problem is that all routes handled first by rails and he redirects you to the page where your react routers are. And you have only one HTML page that contains your react.js code.
When you go to /login or any other page you get err 404 because you don't have a route in rails to handle it.
You need to add rails routes for all your pages and redirect them to the same index page
Or do a catch all routes to the same index page
There's some documentation for configuring your server. Basically you always need to return index.html with a 200 status code.
https://github.com/ReactTraining/react-router/blob/v3/docs/guides/Histories.md#configuring-your-server

Categories