javascript - 如何更新“componentDidUpdate”中的 React 元素?
问题描述
我正在尝试渲染几个依赖于其他反应元素的 x 和 y 坐标的反应元素。我知道我必须更新“componentDidUpdate”方法中的元素,因为在渲染组件之前我无法获取值。
当前行为没有任何更新。renderNew 包含我想要渲染的反应元素,但它没有更新,因为没有显示任何内容。
我没有将值存储在状态中,我认为我不需要。
这是我设置状态和其他变量的地方
constructor(props) {
super(props);
this.state = {
priceDataData: jsonOrders.order.map(order => order.total),
heightData: '',
xElements: '',
yElements: '',
dotsWeWant: ''
};
this.dimensionsX = [];
this.dimensionsY = [];
this.renderNew = [];
this.dotRef = [];
}
这是我的 componentDidUpdate 方法
componentDidUpdate = () => {
const nodeRefs = this.dotRef;
this.dimensionsX = this.connectTheDotsX(nodeRefs);
this.dimensionsY = this.connectTheDotsY(nodeRefs);
this.renderNew = this.renderNewLines(this.dimensionsX,
this.dimensionsY, this.state.priceDataData, this.state.dotsWeWant);
}
}
我的 JSX 的片段,其中包含未更新的变量。第一次渲染时它将为空,需要更新。
<div className="graph_space">
<div className="dot_container">
{this.state.dotsWeWant}
</div>
<div className="test_stuff">
{this.renderNew}
</div>
</div>
解决方案
推荐阅读
- mocha.js - 期望具有深度相等的两个对象之一
- sql - 创建在 SQL 中返回表的函数
- swift - 在添加 LinkPresentation 视图的堆栈上重用单元格时不显示元素
- telosys - Telosys 不生成列定义(即用于 CHAR 列)
- python - Plotly-Dash:如何从 Dash WebApp 中删除状态图标?
- sql - 使用 group by 和 max() 查询不会使用索引
- python - 8 拼图游戏。如何检查和更改代表游戏板图块的目录键值
- node.js - 如何使用 tilda~ 从命令行安装 npm 包?
- erlang - 如何编译包含来自 erl shell 的 Erlang 文件?
- google-app-engine - Google App Engine 网址格式改为显示默认服务