React router React路由器v4:无法读取属性';路线';未定义的

React router React路由器v4:无法读取属性';路线';未定义的,react-router,React Router,我想在按下按钮时重定向,所以我使用with router来访问历史道具 但我得到了一个错误: Uncaught TypeError: Cannot read property 'route' of undefined at Route.computeMatch (react-router.js:1160) 使用路由器HOC包装组件时出错。 如果我用Router功能删除,它就会正常工作 我的代码如下所示: class App extends Component { // ...some u

我想在按下按钮时重定向,所以我使用
with router
来访问历史道具

但我得到了一个错误:

Uncaught TypeError: Cannot read property 'route' of undefined
  at Route.computeMatch (react-router.js:1160)
使用路由器HOC包装组件时出错。 如果我用Router功能删除
,它就会正常工作

我的代码如下所示:

class App extends Component {

// ...some unrelated functions

handleTitleTouchTap = e => {
    e.preventDefault()
    const { history } = this.props
    history.push('/')
}

render() {
                //...other components
        <Router>
            <div>      
                <Switch>
                    <Route exact={true} path="/" component={Home} />
                    <Route path="/search" component={Search}/>
                    <Route path="/gamelist/:listId" component={GameListDetail}/>
                    <Route path="/game/:gameId" component={GameDetail}/>
                    <Route path="/manageuser" component={ManageUser} />
                    <Route path="/addgamelist" component={AddGameList} />
                    <Route path="/addgame" component={AddGame} />
                    <Route path="/test" component={Test} />
                    <Route component={NoMatch} />
                </Switch>
                <LoginForm isLoginFormOpen={isLoginFormOpen} closeLoginForm={closeLoginForm} handleLogin={handleLogin}/>
                <RegisterForm isRegisterFormOpen={isRegisterFormOpen} closeRegisterForm={closeRegisterForm} register={register}/>
            </div>
        </Router>
    )
}


const mapStateToProps = state => ({
    //some props
})
const mapDispatchToProps = dispatch => ({
    //some functions
})
const Container = connect(mapStateToProps, mapDispatchToProps)(App)

export default withRouter(Container)
类应用程序扩展组件{
//…一些不相关的功能
handleTitleTouchTap=e=>{
e、 预防默认值()
const{history}=this.props
history.push(“/”)
}
render(){
//……其他组成部分
)
}
常量mapStateToProps=状态=>({
//一些道具
})
const mapDispatchToProps=调度=>({
//一些功能
})
const Container=connect(mapStateToProps、mapDispatchToProps)(应用程序)
使用路由器导出默认值(容器)

我遇到了同样的问题,我将包装好的组件封装在
路由器
组件(即
浏览器路由器
)中解决了这个问题

在您的示例中,它将变成:

// assuming this file is Container.js
export default withRouter(Container)


// index.js
import Container from './Container'

render(
    <BrowserRouter>
        <Container/>
    </BrowserRouter>,
    document.getElementById('root')
 )
//假设此文件是Container.js
使用路由器导出默认值(容器)
//index.js
从“./Container”导入容器
渲染(
,
document.getElementById('root'))
)
此处文档中的工作示例:


我还打开了一个关于回购协议的问题,因为我认为文档中的示例不够清晰。

完全相同的问题您在这里使用的是
?Hi@leuction,我的回答有没有帮助您?我也使用react路由器redux解决这个问题。这是一个非常奇怪的错误。