Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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 ';this.props.history.push();是否适用于普通页面,但不适用于嵌套组件?_Reactjs_React Redux_React Router - Fatal编程技术网

Reactjs ';this.props.history.push();是否适用于普通页面,但不适用于嵌套组件?

Reactjs ';this.props.history.push();是否适用于普通页面,但不适用于嵌套组件?,reactjs,react-redux,react-router,Reactjs,React Redux,React Router,我有一个名为Addmember.js的页面,在那里我可以添加人。我有一个“下一步”按钮,通过下面的代码将我带到仪表板 this.props.history.push('/dashboard') 但是真正的问题出现了,当我在一个新页面中使用addmember组件时,当我单击next时,它会显示下面的错误 无法读取未定义的属性“push” 谢谢你的帮助 尝试使用路由器从“react router dom”导入 然后在withRouter中传递组件 例如 export default withRo

我有一个名为Addmember.js的页面,在那里我可以添加人。我有一个“下一步”按钮,通过下面的代码将我带到仪表板

 this.props.history.push('/dashboard')
但是真正的问题出现了,当我在一个新页面中使用addmember组件时,当我单击next时,它会显示下面的错误

无法读取未定义的属性“push”


谢谢你的帮助

尝试使用路由器从“react router dom”导入

然后在withRouter中传递组件

例如

export default withRouter(Addmember);

问题是,当您嵌套组件时,它找不到历史道具。 目前react路由器dom已更新到版本5,因此您可以使用它们的挂钩

要解决给定的问题,您应该像从“react router dom”导入{useHistory}一样导入useHistory钩子。


然后在AddMember中使用它,如
const history=useHistory()
。然后,您可以在需要时使用历史记录中的所有方法。我想这会对你有所帮助

你的嵌套组件是什么样子的?为什么不直接使用
链接
来代替呢?我使用的是一个分类组件,挂钩只能在功能组件内部使用完美的巴迪…搞定了..Tnks