# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/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](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](https://facebook.github.io/create-react-app/docs/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](https://facebook.github.io/create-react-app/docs/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](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](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](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](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](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](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](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
react v6路由钩子函数
需积分: 0 141 浏览量
更新于2022-12-12
收藏 168KB ZIP 举报
在React V6中,路由钩子函数是React Router库的核心组成部分,它们允许我们在组件中轻松地与路由系统交互。React Router V6引入了一些新的API,这些API以React的钩子形式提供,使得在函数组件中处理路由变得简单而直观。本篇文章将详细探讨React Router V6中的关键路由钩子函数及其用法。
最重要的两个路由钩子是`useHistory`和`useLocation`。在React Router V5中,我们通常会使用这两个来管理路由状态和导航。然而,在V6中,它们被一个更强大的钩子`useNavigate`所替代。`useNavigate`提供了向URL导航的能力,类似于`history.push`或`history.replace`。例如:
```jsx
import { useNavigate } from 'react-router-dom';
function MyComponent() {
const navigate = useNavigate();
function handleClick() {
navigate('/some/path');
}
return <button onClick={handleClick}>Go to Some Path</button>;
}
```
接下来,`useParams`钩子用于获取当前路由参数。在定义路由时,我们可能使用动态路径段(如`:id`),`useParams`能让我们在组件内部访问这些参数。例如:
```jsx
import { useParams } from 'react-router-dom';
function Profile() {
const { id } = useParams();
// Now you can use the ID to fetch user data or perform other operations
return <div>User ID: {id}</div>;
}
```
`useLocation`虽然在V6中仍然存在,但它的使用不如V5广泛,因为`useNavigate`和`useParams`通常能满足大部分需求。它返回一个`location`对象,包含当前URL的信息,如`pathname`, `search`, 和 `hash`。
除此之外,`useRouteMatch`在V5中用于检查当前URL是否匹配某个路由模式。在V6中,这个功能被`useRoutes`所取代,它允许你定义一个路由配置数组,然后根据当前URL解析出匹配的路由。这是一个更强大的方式来组织和管理你的路由结构。
```jsx
import { useRoutes } from 'react-router-dom';
const routes = [
{ path: '/', element: <Home /> },
{ path: '/profile/:id', element: <Profile /> },
];
function App() {
const routesRendered = useRoutes(routes);
return routesRendered;
}
```
`useContext`钩子虽然不是React Router特有的,但它可以帮助我们访问`RouterContext`,尤其是当我们需要在非组件代码中获取路由信息时。然而,这通常是不必要的,因为大多数情况下,我们可以通过上述的路由钩子实现相同的功能。
总结来说,React Router V6的路由钩子函数简化了路由管理,并使函数组件的路由逻辑更加清晰。通过`useNavigate`、`useParams`、`useLocation`和`useRoutes`,我们可以高效地控制应用的导航、获取URL参数、了解当前路由状态以及构建复杂的路由结构。在实践中,这些钩子极大地提高了代码的可读性和可维护性,是现代React开发中不可或缺的工具。