reactjs - 如何在反应中使用带参数的函数作为道具
问题描述
如果我们使用带参数的函数并希望将此函数用作其他组件的道具,并且当我尝试console.log(this.props)
在其他组件中使用时,我会得到未定义。
anotherfunctionCall = (ss) => {
console.log(ss)
}
<DrawerRight
anotherfunctionCall = {this.anotherfunctionCall('some')}
/>
当我在其他组件中控制台(this.props)时,我得到未定义。请帮助解决这个问题。如果我使用 anotherfunctionCall = (ss) => () => { console.log(ss) } 它正在工作
解决方案
我不确定这是否是您正在寻找的答案,但是您将一个函数作为道具传递给一个没有任何参数的组件。您只需传递函数引用并从带有或不带有道具的子组件中调用它。(无论你如何定义它)
anotherfunctionCall = (ss) => {
console.log(ss)
}
<DrawerRight
anotherfunctionCall = {this.anotherfunctionCall}
/>
//DrawerRight.js
...
componentDidMount(){
this.props.anotherfunctionCall("test");
}
...
推荐阅读
- node.js - 推送通知的 Firebase 功能 - “错误:需要主题、令牌或条件之一”
- rust - 自定义窗口管理器:一些 GTK+ 3 窗口接收焦点但不接受鼠标点击
- python-3.x - Python 3 触发隐式值更改的回调
- .net - WebView{1} 开发机器 VS 部署 VB.net
- php - 使用联系表单 Submission.php 接收 500 服务器错误
- java - 如何使用 Spring Framework Aggregation 返回包含不同值的完整文档?
- javascript - “YYYY-MM-DDThh:mm:ss”格式日期在 Safari 上不起作用
- python - 迭代从 yaml 加载的 python 字典
- excel - 如何在列中查找非精确字符串匹配?
- php - 当我有 3 个表时,我如何设法使用“addCondition”(Yii1)