运行/执行功能,然后转到reactjs中的Route

时间:2019-09-18 05:26:58

标签: reactjs react-router-dom

  • 我创建了一个react应用并实现了两条路线。

  • “ /”上的页面显示欢迎消息,/ pageone显示另一个 页面。

  • 我在主页(“ /”)中有一个按钮,该按钮包装有链接 to =“ / pageone”。

  • 我有一个函数onClose = () => {this.setState({visible:false,});},我希望它先运行,之后才想更改我的路线onclicking /

如何实现这一目标?有什么方法可以编写一个运行此onClose函数然后路由到链接的函数?

1 个答案:

答案 0 :(得分:1)

您的查询与回调函数有关,因此您可以像这样在this.setState中使用回调函数

this.setState({
    visible:false 
},() => {
    /// this part will be executed only after the visible is set to false
});