You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Router 4路由问题:URL参数异常致404跳转失效

求助:React Router 4 404跳转异常问题

各位大佬好!先提前谢过大家的帮忙,StackEx真的太好用了!我连着几天排查、编码、测试,现在卡在两个相关问题里的小问题上,最紧急的是一个React测试失败的情况:

  • /hello/conehead:能正确返回"Hello conehead.",测试顺利通过 :)
  • 404页面跳转整体正常,但有个例外情况:访问/hello/cone/head?的时候,按预期应该触发失败跳转,但它就是没跳到404页面 :(

我用的是React Router 4,本来想着得提取location参数,但这一步显然没做好。之后我还可以优化触发404跳转的条件逻辑,但现在先解决眼前的问题。

现在最核心的是想知道怎么正确打印router.location元素!!!

相关代码如下:

import React from 'react';
import { Redirect, Match } from "react-router-dom";
export default ({ match: { params: { name }}}) => {
 console.log(name); // debug router match or location params ??
 if (name.indexOf("/") > -1) {
  return <Redirect to={{ pathname: "/404" }} />;
 } else {return (<h2>Hello {name}</h2>); }
}

内容的提问来源于stack exchange,提问作者Jeremy Donson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:02:31