1 Star 0 Fork 0

qinpmc/react-router-6_tutorial

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Getting Started with Create React App

This project was bootstrapped with Create React App.

Available Scripts

In the project directory, you can run:

npm start

Runs the app in the development mode.
Open http://localhost:3000 to view it in your browser.

The page will reload when you make changes.
You may also see any lint errors in the console.

npm test

Launches the test runner in the interactive watch mode.
See the section about running tests for more information.

npm run build

Builds the app for production to the build folder.
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.
Your app is ready to be deployed!

See the section about deployment for more information.

npm run eject

Note: this is a one-way operation. Once you eject, you can't go back!

If you aren't satisfied with the build tool and configuration choices, you can eject at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

Learn More

You can learn more in the Create React App documentation.

To learn React, check out the React documentation.

Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

npm run build fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

注意

  1. App中没有用一个根元素包裹 Routes,居然无法正确渲染,且控制台无提示
  2. 6.0的react-router 版本官方已经不再支持activeClassName这种写法

钩子函数

  1. useParams 获取路由参数
  2. useNavigate 编程式路由
export const Details = () => {
  let params = useParams();
  let navigate = useNavigate();

  return (
    <div>
      <p>{params.type} 详情内容</p>
      <br /> 
      <button onClick = {() =>{
        // navigate("../")
        navigate("/products")
      }}> 后退</button>
    </div>

  ) 
}


// 路由
<Route path='products' element={<Products />} >
  <Route path='details/:type' element={<Details />} />
</Route>



// 公司产品组件
export const Products = () => (
  <section className="products">
    <Link to='/products/details/telphone'>手机</Link>
        &nbsp;
    <Link to='/products/details/computer'>电脑</Link> 
    <Outlet />
  </section>
)
 

  1. useLocation 获取pathname等

// 联系我们组件
export const Contact = () => {
  
  const location = useLocation();
  console.log(location)
  const {  pathname } = location
  return (
    <section className="contact">
      <h1>联系我们</h1>
      <p>公司电话:0755 - 12345678</p>
      <p> 你到了   {pathname}</p>
    </section>
    
  )
}

父子路由 以及默认子路由

  1. 是Route 嵌套起来,实现子路由
  2. 子路由的路径为 父级路由路径+子级路由路径,
  3. 默认子级路由添加 index

<Route path = '/' element = {<Home />} > 
  <Route index element={<About/>} /> 
  <Route path='about' element={<About />} />
  <Route path='contact' element={<Contact />} />
  <Route path='products' element={<Products />} >
    <Route path='details/:type' element={<Details />} />
</Route>

NavLink

  1. NavLink 组件, 它和Link功能是一样的,都是标识请求,只是使用的场景不一样。navLink 提供了一个isActive 属性,可以设置高亮样式
  2. V5 和V6的不同 , 去掉了activeClassName 和 activeStyle, 要使用isActive

空文件

简介

react-router-6_tutorial 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qinpmc/react-router-6_tutorial.git
git@gitee.com:qinpmc/react-router-6_tutorial.git
qinpmc
react-router-6_tutorial
react-router-6_tutorial
master

搜索帮助