简体   繁体   中英

adding a router with tailwind and react 6

React 6 has done away with switch but I am not sure how to implement a router with tailwind or html tags. Currently I am getting an error that reads.

router.ts:5 Uncaught Error: useHref() may be used only in the context of a <Router> component.

but this is confusing and not very descriptive.

import React, {Component, Fragment} from 'react';
import {BrowserRouter, Routes, Route, Link} from "react-router-dom";
import About from "../about/about";
import Home from "../home/home";
import Blog from "../blog/blog";
import Contact from "../contact/contact";
import Tutorials from "../tutorials/tutorials";

export default class Header extends Component {

  render() {
      return (
              <nav className="flex items-center justify-between flex-wrap bg-blue-500 p-6">
                  <div className="flex items-center flex-shrink-0 text-white mr-6">
                      <svg className="fill-current h-8 w-8 mr-2" width="54" height="54" viewBox="0 0 54 54"
                           xmlns="http://www.w3.org/2000/svg">
                          <path
                              d="M13.5 22.1c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05zM0 38.3c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05z"/>
                      </svg>
                      <span className="font-semibold text-xl tracking-tight">Fullstack Guru</span>
                  </div>
                  <div className="block lg:hidden">
                      <button
                          className="flex items-center px-3 py-2 border rounded text-teal-200 border-teal-400 hover:text-white hover:border-white">
                          <svg className="fill-current h-3 w-3" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                              <title>Menu</title>
                              <path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z"/>
                          </svg>
                      </button>
                  </div>
                  <div className="w-full block flex-grow lg:flex lg:items-center lg:w-auto">
                      <div className="text-sm lg:flex-grow">
                          <a href="#responsive-header"
                             className="block mt-4 lg:inline-block lg:mt-0 text-teal-100 hover:text-white mr-5">
                              <Link to="/home">Home</Link>
                          </a>
                          <a href="#responsive-header"
                             className="block mt-4 lg:inline-block lg:mt-0 text-teal-100 hover:text-white mr-5">
                              <Link to="/tutorials">Tutorials</Link>
                          </a>
                          <a href="#responsive-header"
                             className="block mt-4 lg:inline-block lg:mt-0 text-teal-100 hover:text-white mr-5">
                              <Link to="/blog">Blog</Link>
                          </a>
                          <a href="#responsive-header"
                             className="block mt-4 lg:inline-block lg:mt-0 text-teal-100 hover:text-white mr-5">
                              <Link to="/about">About</Link>
                          </a>
                          <a href="#responsive-header"
                             className="block mt-4 lg:inline-block lg:mt-0 text-teal-100 hover:text-white mr-5">
                              <Link to="/blog">Blog</Link>
                          </a>
                          <a href="#responsive-header"
                             className="block mt-4 lg:inline-block lg:mt-0 text-teal-100 hover:text-white mr-5">
                              <Link to="/contact">Contacts</Link>
                          </a>
                      </div>
                      <div>
                          <a href="#"
                             className="inline-block text-sm px-4 py-2 leading-none border rounded text-white border-white hover:border-transparent hover:text-teal-500 hover:bg-white mt-4 lg:mt-0">Login / Register</a>
                      </div>
                  </div>
              </nav>
      );
  }
}

function r() {
    return(
        <BrowserRouter>
            <Routes>
                <Route path="/" element={<Home/>}/>
                <Route path="/tutorials" element={<Tutorials/>}/>
                <Route path="/about" element={<About/>}/>
                <Route path="/blog" element={<Blog/>}/>
                <Route path="/contact" element={<Contact/>}/>
            </Routes>
        </BrowserRouter>
    )
}

The error is telling you exactly what the issue is. You are using something that needs to be within your <Router/>

I am assuming the issue is coming from the the <Header/> which right now doesn't look like it is being mounted within the router. Since you are using the Link component in your <Header/> then it needs to be within the <BrowserRouter> so it knows where to navigate to.

function r() {
    return(
        <BrowserRouter>
           
            <Header/>

            <Routes>
                <Route path="/" element={<Home/>}/>
                <Route path="/tutorials" element={<Tutorials/>}/>
                <Route path="/about" element={<About/>}/>
                <Route path="/blog" element={<Blog/>}/>
                <Route path="/contact" element={<Contact/>}/>
            </Routes>
        </BrowserRouter>
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM