Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Can';无法从id路由获取参数_Reactjs_React Router_History - Fatal编程技术网

Reactjs Can';无法从id路由获取参数

Reactjs Can';无法从id路由获取参数,reactjs,react-router,history,Reactjs,React Router,History,我的项目遇到了一个问题。我试图显示一个组件并使用this.props.match.params,但无论我做什么,我都会得到未定义的 路线: const App = () => ( <BrowserRouter> <Fragment> <Header/> <main> <Switch> &l

我的项目遇到了一个问题。我试图显示一个组件并使用this.props.match.params,但无论我做什么,我都会得到未定义的

路线:

const App = () => (
    <BrowserRouter>
        <Fragment>
            <Header/>
            <main>
                <Switch>
                    <Route path="/show/:id" component={Show}/>
                    <Route path="/" component={Home}/>
                </Switch>
            </main>
        </Fragment>
    </BrowserRouter>
);

export default App;
最后在我的节目部分

   componentDidMount() {
            console.log(this.props.match.params)
            const {id} = this.props.match.params;
            if (id) {
                this.props.requestFindDwelling(id);
            }
        }
所以我一直在研究,我认为这不是react路由器的问题,首先,当我试图通过键入路由来访问路由时,我在bundle.js上得到了意想不到的>,通过在index.html上添加
,解决了这个问题。 现在,我的组件正在通过console呈现ok。show组件的日志提供了以下信息:

isExact:false
params:{}
path:"/show"
url:"/show"
当我启动项目以便能够使用browserhistory并通过刷新页面而不出错时,我必须将此添加到我的索引文件中:

app.get('/*', function(req, res) {
    res.sendFile(path.join(__dirname, './public/index.html'), function(err) {
        if (err) {
            res.status(500).send(err);
        }
    });
});

对于我遇到的这种错误,我假设没有找到路由,正在将我重定向到/show。

这解决了它,这是一个程序问题,我花了几个小时试图修复它哈哈简单,甚至不需要确切的
<Switch>
    <Route path="/show/:id" component={Show}/>
    <Route path="/" component={Home}/>
</Switch>
<Switch>
    <Route path="/show/:id" component={Show}/>
    <Route path="/" component={Home}/>
</Switch>
<Switch>
    <Route exact path="/" component={Home}/> // exact is important
    <Route path="/show/:id" component={Show}/>
</Switch>