【问题标题】:How can I pass function through Link in React?如何通过 React 中的链接传递函数?
【发布时间】:2020-06-29 17:20:50
【问题描述】:

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

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

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

这就是我调用函数的方式

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

但我有这个错误

history.js:370 未捕获的 DOMException:无法在“历史”上执行“pushState”:function () { console.log("work"); } 无法克隆。

我该如何解决? 非常感谢

【问题讨论】:

  • 您能否添加更多信息以帮助您?
  • 如果我将 LInk 状态数字或字符串放入它是通过但是当我尝试传递函数时我有一个错误。我认为必须有一个特殊的方法或类似的东西来传递一个函数。什么你想知道什么样的信息?
  • 这能回答你的问题吗? stackoverflow.com/questions/52786072/…
  • 我觉得这个可以解决你的问题Link
  • 有帮助,谢谢)

标签: reactjs react-router jsx


【解决方案1】:

当你用数据替换状态时可以做到这一点。

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

这里也替换

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

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

【讨论】:

    猜你喜欢
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 2022-01-19
    • 2020-11-05
    • 2021-12-12
    • 2015-04-04
    • 2020-08-19
    相关资源
    最近更新 更多