reactjs - 如何在 react 的 render 方法中调用方法
问题描述
我遇到了 React nooob 问题。问题是我需要在方法中调用一个名为 namexxx
的render
方法,但我不知道如何。
这是方法:
xxx() {
return (
this.state.dataLoadedQuote &&
<BlogBandLeadershipBlogQuoteItem item={this.state.blogQuote} labels={this.props.labels} />
);
}
解决方案
您似乎xxx()
正确地调用了您的函数(尽管您不需要传递参数0
- 这是不必要的)。问题可能在于,由于您在this
内部使用xxx()
,因此需要在构造函数中绑定函数。否则,this
将不会引用您的组件,因此this.state.dataLoadedQuote
将始终是虚假的。
所以添加:
this.xxx = this.xxx.bind(this);
您的构造函数现在可能看起来像:
constructor() {
super();
this.state = {}
this.xxx = this.xxx.bind(this);
}
推荐阅读
- python - 使用Python重命名标题中带引号的文件
- django - 如何让 Django 单元测试检查 M2M DB 约束?
- javascript - 如何在不实际聚焦元素的情况下触发焦点处理程序?
- java - InetAddress.getAddress() 总是返回 null,但不知何故仍然有效
- node.js - 在节点 js 按钮单击更改网页
- r - GINI 函数无法识别数据帧(包 reldist)
- sql - 如何在 PCF 上启用 sql server 连接
- c# - 将 SPLitsItem 数据转换为某种图表或图形
- c# - 使用 iTextSharpes 压缩 PDF 图像导致内存不足
- javascript - 基于所选选项的 PDF 导航到特定页面