如何通过Reaction中的Link传递函数? [英] How can I pass function through Link in React?

查看:20
本文介绍了如何通过Reaction中的Link传递函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过Link将函数传递给另一个组件。

    testFavorite=()=>{
        console.log("work")
    }

    <Link 
       to={{
       pathname: '/popular',
         state:{
            testFavorite: this.testFavorite
         }
       }}>
   Popular</Link>

这就是我调用函数的方式

this.props.location.state.testFavorite();

但我有此错误

History y.js:370未捕获DOMException:无法对‘History’执行‘presState’:Function(){console.log("work");}无法克隆。

我如何才能修复它? 非常感谢

推荐答案

可以在用数据替换状态时完成。

<Link 
       to={{
       pathname: '/popular',
         data:{
            testFavorite: this.testFavorite
         }
       }}>

同时替换此处

this.props.location.state.testFavorite();

同时,如果您还希望将数据作为道具进行传递,您可以将状态与数据一起使用

这篇关于如何通过Reaction中的Link传递函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆