gpt4 book ai didi

javascript - React router dom 库不适用于我的项目

转载 作者:行者123 更新时间:2023-12-02 22:33:00 24 4
gpt4 key购买 nike

我的名字是拉吉迪普·辛格。我在我的 React 项目中使用了 React-router-dom 。单击链接标签时,链接标签不起作用。我不明白为什么不起作用。在浏览器中更改 URL,但更改组件,而不是在 Web 应用程序上更改

检查我的代码这是我的 Main.js 组件文件代码

import React, { Fragment } from 'react';
import Foot from './Footer';
import Head from './Header'
import MainContect from './MainContect';
import About from './page/About';
import Book from './page/Book'
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";


function Main() {
return (

// use Fragment
<Fragment>

{/* use BrowserRouter as Router */}
<Router>

{/* out Router use Head componet */}
<Head />


{/* use Switch tag */}
<Switch>

{/* use Route defined Router path */}

<Route path="/" exact >

{/* compnent name */}
<MainContect />

</Route>

<Route path="/book" >

<Book />

</Route>
<Route path="/about" >
<About />
</Route>
</Switch>

{/* out Router use Head componet */}
<Foot />
</Router>

</Fragment>

)
}
export default Main

这是我的 Header.js 组件文件代码

import React from 'react';
import './css/header.scss';

import { BrowserRouter as Router, Link } from 'react-router-dom';


function Head() {
return (
// Use Router tag wrap all Link in Router
<Router>
<header>
<h1 className='logo'> Sikh Book</h1>
<div className="iconArea">+</div>
<ul className="ulArea">


<li>
{/* Use Link tag for navigation */}
<Link to="/"> Home </Link> </li>
<li>
<Link to="/book">
Book
</Link>
</li>

<li>
<Link to="/about">
About
</Link>
</li>


</ul>
</header>
</Router>

)
}
export default Head

请告诉我我的错误是什么谢谢你帮助我

最佳答案

您应该仅启动一次 BrowserRouter。我会在组件树的较高级别上执行此操作。你可以看一下这个例子。

// BrowserRouter is the router implementation for HTML5 browsers (vs Native).
// Link is your replacement for anchor tags.
// Route is the conditionally shown component based on matching a path to a URL.
// Switch returns only the first matching route rather than all matching routes.
import {
BrowserRouter as Router,
Link,
Route,
Switch,
} from 'react-router-dom';
import React from 'react';

const Home = () => <h1>Home</h1>;
const About = () => <h1>About</h1>;

// We give each route either a target `component`, or we can send functions in `render` or `children`
// that return valid nodes. `children` always returns the given node whether there is a match or not.
const App = () => (
<Router>
<div>
<Link to="/">Home</Link>{' '}
<Link to={{pathname: '/about'}}>About</Link>{' '}
<Link to="/contact">Contact</Link>

<Switch>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
<Route
path="/contact"
render={() => <h1>Contact Us</h1>} />
<Route path="/blog" children={({match}) => (
<li className={match ? 'active' : ''}>
<Link to="/blog">Blog</Link>
</li>)} />
<Route render={() => <h1>Page not found</h1>} />
</Switch>
</div>
</Router>
);

致谢:siakaramalegos

关于javascript - React router dom 库不适用于我的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58832778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com