Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 React路由器V4无法获取匹配参数_Reactjs_React Router - Fatal编程技术网

Reactjs React路由器V4无法获取匹配参数

Reactjs React路由器V4无法获取匹配参数,reactjs,react-router,Reactjs,React Router,我使用React路由器V4来学习React for初学者教程。但是我无法在我的组件中获取参数。这是我的部分代码 const Root = () => { return ( <BrowserRouter> <div> <Switch> <Route exact path='/' component={StorePicker} /> <Route Path='store/:st

我使用React路由器V4来学习React for初学者教程。但是我无法在我的组件中获取参数。这是我的部分代码

const Root = () => {
return (
  <BrowserRouter>
    <div>
      <Switch>
          <Route exact path='/' component={StorePicker} />
          <Route Path='store/:storeId' component={App} />
          <Route component={NotFound} />
      </Switch>
    </div>
  </BrowserRouter>
)
}

我的代码出了什么问题?

React路由器4已重新启用:

路由器上下文不再自动注入到道具中(如果我没记错,这就是react router 3的工作方式,或者取决于您如何指定路由器呈现的组件的上下文)

试试这个:

从“react router”导入{withRouter}

导出零部件时,请将其包裹:

withRouter(MyComponent)

这应该正确地注入你正在寻找的道具,因此它们不是未定义的

我直接从react router站点获取:


你能提供整个StorePicker组件文件吗?@AnkitParikh我现在已经更新了我的代码它需要是
?@bennygenel不,这只是一个实践项目,我想我不清楚。我想说的是,这可能与你的路径上缺少一条斜线有关
path='store/:storeId'
可能需要是
path='/store/:storeId'
,所以我应该像导出默认withRouter(App)一样导出我的应用程序组件?我不太确定,但
withRouter
适用于与路由没有直接关系的组件。您可以使用路由器
而不是将道具传递给每个孩子,因此请在顶部声明您的类,例如:
类应用程序扩展React.Component
。然后在底部的
导出默认withRouter(App)
,假设我不知道你的应用程序设置,但我假设这是你的问题,为什么它的未定义调整检查了你的代码,并且假设它工作,你会想在你的
StorePicker
组件中导入withRouter,看到你提到的
App
。我仍然无法获取路由器参数。
class StorePicker extends React.Component {
    render() {
        return (
            <form className="store-selector" onSubmit={(e) => this.goToStore(e)}>
                <h2>Please Enter A Store</h2>
                <input type="text" ref={(input) => { this.storeInput= input; }} defaultValue={getFunName()} placeholder="Store Name" required/>
                <button type="submit">Visit Store</button>
            </form>
        )
    }

    goToStore(event) {
        event.preventDefault()
        const storeInput = this.storeInput.value
        console.log(storeInput)
        this.props.history.push(`store/${storeInput}`)
    }

}
//storeId is undefine
this.props.match.params.storeId