reactjs - 如何从 react-router-dom 索引中获取参数?
问题描述
我需要从 '/:ref' 获取 ref 参数并重定向到家庭路由器 '/home'
const App: FC = () => {
...
return (
<IntlProvider locale={language} messages={MESSAGES[language]}>
<IonApp>
<IonReactRouter>
<Toolbar />
<Menu />
<IonRouterOutlet id="menuContent">
<Route path={ROUTES.DASHBOARD} component={Dashboard} exact />
<Route path={ROUTES.HISTORY} component={HistoryOrders} exact />
<Route path={ROUTES.HISTORY_ORDER_ID} component={OrderById} exact />
<Route path={ROUTES.CART} component={Cart} exact />
<Route path="/:ref" component={IndexRef} exact />
</IonRouterOutlet>
</IonReactRouter>
</IonApp>
</IntlProvider>
)
}
import { useParams, useHistory } from 'react-router-dom'
const IndexRef: FC = () => {
const { ref } = useParams<IIndexRefParams>()
const history = useHistory()
const IonStorage = useContext(IonStorageContext)
useEffect(() => {
const setRefToStorage = async (shop: string) => {
await IonStorage.set(SHOP_REF, shop)
}
if (ref) {
setRefToStorage(ref)
history.push(ROUTES.DASHBOARD)
}
}, [])
return (
<PageContainer>
<h1>
<FormattedMessage id="index.wrongLink" />
</h1>
</PageContainer>
)
}
问题是所有其他路由都匹配“/”。我无法让任何其他路由器收集此“ref”参数,因为指向此路径的重定向链接会创建第三方服务。解决方法是什么?
解决方案
推荐阅读
- php - 如何在 PHP 中将列表制作成树形结构
- python - 熊猫日期时间灾难,无法将日期行转换为任何统一可用的东西
- c# - 有没有其他方法可以注册实现以使其可被任何派生接口注入?
- jquery - 不推荐使用 jquery isFunction
- gradle - 如何在许多战争项目之间共享静态内容(使用工件?)
- linux - 模块 init 函数只在 linux 设备驱动程序中打印,驱动程序是否在设备树解析期间初始化?
- python - 在 Python 中使用控制台选择和更改全局变量
- android - 材质底部导航底部图标空间bug
- sql - 根据 SQL (Oracle) 中的预定义日期范围对记录进行分组
- javascript - 如果部分中的摘要块为空,则隐藏 Squarespace 页面部分