如何使用React路由器更改重新渲染的文档标题名称?
我基本上希望我的文档标题在显示更改的数组的值时进行更新 - 我在屏幕上呈现订单列表,当用户按下按钮时,它将更改,但是在屏幕上显示了许多订单。
我希望在文档标题中反映的产品数量:“/(订购)订单”将是React路由器中路由的名称。订购将在每次重新渲染显示的新订单上更改。
但是,当我收到错误/警告时,它不起作用:(没有路由与位置匹配的位置“/()订单”)
如何解决这个问题?
下面的代码。
orders.jsx
import { Col, Row } from "antd";
import { useContext, useEffect, useMemo, useState } from "react";
import Order from "../components/Order";
import { AppContext } from "../context/Context";
import AntButton from "../elements/Button";
import ordersToDisplay from "../orders.json";
const OrdersPage = () => {
const [filteringStatus, setFilteringStatus] = useState("");
const {orderAmount, setOrderAmount} = useContext(AppContext)
const [test, setTest] = useState("")
const setDiplayAcceptedOrders = () => {
setFilteringStatus("accepted");
};
const setDiplayCompletedOrders = () => {
setFilteringStatus("complete");
};
const setDiplayInProgressOrders = () => {
setFilteringStatus("inProgress");
};
const orders = useMemo(() => {
if (filteringStatus) {
return ordersToDisplay.filter((i) => i.orderStatus === filteringStatus);
}
return ordersToDisplay;
}, [filteringStatus]);
useEffect(() => {
console.log(orderAmount);
console.log(orders.length)
setOrderAmount(orders.length)
})
console.log("Orders to display: ", ordersToDisplay);
console.log("test value: ", test)
return(
<div className="container">
<Row justify="space-between" align="middle">
<Col span={6}>
<h1>Orders</h1>
</Col>
<Col span={6}>
<AntButton onClick={setDiplayAcceptedOrders} name="Accepted"/>
</Col>
<Col span={6}>
<AntButton onClick={setDiplayInProgressOrders} name="In Progress"/>
</Col>
<Col span={6}>
<AntButton onClick={setDiplayCompletedOrders} name="Complete"/>
</Col>
</Row>
<Row>
<Col span={12}>
<h3>{filteringStatus == "" ? "All Orders" : filteringStatus}</h3>
{orders.map((e) => {
return(
<Order
key={e.id}
productName={e.productName}
dateOrdered={e.dateOrdered}
orderStatus={e.orderStatus}
/>
)
})}
</Col>
</Row>
</div>
)
}
export default OrdersPage;
app.jsx
import './styles/styles.scss'
import orders from "./orders.json";
import { BrowserRouter } from 'react-dom';
import HomePage from "./pages/home";
import OrdersPage from "./pages/orders";
import { useRoutes } from 'react-router-dom';
import { useState } from 'react';
import { AppContext } from './context/Context';
const App = () => {
const [orderAmount, setOrderAmount] = useState("")
const Routes = useRoutes([
{path: "/", element: <HomePage/>},
{path: `/(${orderAmount})orders`, element: <OrdersPage/>}
])
return (
<AppContext.Provider value={{
orderAmount, setOrderAmount
}}>
<div>
{Routes}
</div>
</AppContext.Provider>
);
};
export default App;
home.jsx(用户最初单击按钮将其带到订单页面
import { Col, Row } from "antd";
import { useContext } from "react";
import { Link } from "react-router-dom";
import { AppContext } from "../context/Context";
import AntButton from '../elements/Button'
const HomePage = () => {
const {orderAmount} = useContext(AppContext)
console.log(orderAmount)
return(
<div className="container">
<Row>
<Col span={24}>
<Link to={`/(${orderAmount})orders`}>
<AntButton name="Orders" type="primary"/>
</Link>
</Col>
</Row>
</div>
)
}
export default HomePage;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
路径中的支架正在引起问题。根据文档,
如果您坚持使用括号,则可以
或者可以逃脱它,如果您只希望doc标题反映订单的信息,则可以查看 https://github.com/nfl/reaect-helmet ,它允许您从编程中更改DOC标头到任何内容想要不修改路线。
The bracket in the path is causing the problem. Per the docs,
https://github.com/remix-run/react-router/blob/v3/docs/guides/RouteMatching.md
If you insist on using the bracket you can escape it with
Alternatively, if you just want the doc title to reflect order's information, you can check out https://github.com/nfl/react-helmet, which allow you to programmatically change the doc header to whatever you want without modifying the route.