Hola, estoy aprendiendo a reaccionar y tengo algunos problemas. No mostraré el elemento de error cuando la URL no coincida con ninguna ruta.
Creo el elemento ErrorPage pero sigo recibiendo esta advertencia "Ninguna ruta coincide con la ubicación".
Aquí está mi código que escribí:
import React, { Component } from "react"; import { BrowserRouter as Router, NavLink, Route, Routes, } from "react-router-dom"; import "./App.css"; const Home = () => <h1>Home</h1>; const News = () => <h1>News</h1>; const Contact = () => <h1>Contact</h1>; const ErrorPage = () => <h1>Page not found</h1>; class App extends Component { state = {}; render() { return ( <Router> <div> <header> <nav> <ul> <li> <NavLink to="/">Home</NavLink> </li> <li> <NavLink to="/news">News</NavLink> </li> <li> <NavLink to="/contact">Contact</NavLink> </li> </ul> </nav> </header> <section> <Routes> <Route path="/" element={<Home />} /> <Route path="/news" element={<News />} /> <Route path="/contact" element={<Contact />} /> <Route element={<ErrorPage />} /> </Routes> </section> </div> </Router> ); } } export default App;reemplazando
<Route element={<ErrorPage />} />
con
<Route path="*" element={<ErrorPage />} />
haría el truco.