gpt4 book ai didi

javascript - 哪种方式更好地将函数引用发送到 react 组件?

转载 作者:行者123 更新时间:2023-12-03 01:14:41 28 4
gpt4 key购买 nike

我正在尝试将函数引用发送到导入的组件。所以我最好使用 ref 属性或 props ?

我想找到一个最佳且标准的方法。

<MyComponent ref="ListView" />

或者这个方法:

<MyComponent show="this.showModal" hide="this.hideModal" />

最佳答案

如果将函数作为字符串传递(例如 "this.showModal"),则不会产生任何效果。

将它们放在大括号中传递:

<MyComponent show={this.showModal} hide={this.hideModal} />

Refs 的目的是直接操作 DOM,而不是向组件传递某些内容。例如,您可能想使用 jQuery 操作 DOM

关于javascript - 哪种方式更好地将函数引用发送到 react 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52072868/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com