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 如何在react路由器4中获取上一条路径_Reactjs_React Router - Fatal编程技术网

Reactjs 如何在react路由器4中获取上一条路径

Reactjs 如何在react路由器4中获取上一条路径,reactjs,react-router,Reactjs,React Router,使用React路由器4。是否有办法查看堆栈中的上一个路由?我不想去那里,我只想知道用户来自哪里。谢谢您可以使用来获取上一个位置 componentWillReceiveProps(nextProps) { if (nextProps.location !== this.props.location) { this.setState({ prevPath: this.props.location }) } } OP明确表示他们不想去那里,只是想知道路线是什么。我很确

使用React路由器4。是否有办法查看堆栈中的上一个路由?我不想去那里,我只想知道用户来自哪里。谢谢

您可以使用来获取上一个位置

 componentWillReceiveProps(nextProps) {
    if (nextProps.location !== this.props.location) {
     this.setState({ prevPath: this.props.location })
    }
 }

OP明确表示他们不想去那里,只是想知道路线是什么。我很确定这个.props.location实际上只是一个对象。。。此外,当你在react Router中从一条路线导航到下一条路线时,componentWillReceiveProps实际上不会运行。我认为这会起作用,但你需要有一个始终安装的隐藏跟踪组件,以便它知道过去的位置。然后,您可以将数据从那里传递到redux,或者使用React上下文从另一个组件进行访问。从理论上讲,这听起来不错,但老实说,这似乎真的很额外。您可能认为react router dom会更容易实现这一点。这似乎不起作用。我认为它需要上面提到的虚拟跟踪组件。您愿意使用中间件/react router 4旁边的附加库吗?当然,我想。我们可以在多个地方使用这些信息。