reactjs - 路由更改后刷新组件
问题描述
我有一排按钮,所有链接到正在呈现的图表,然后按下按钮,它决定哪些数据将显示在下面的图表上。
<div>
<Route path="/" component={Main} />
<Route path="/chart/:topic" component={Chart} />
</div>
按钮元素:
<Link to={"/chart/" + collection.name}>
<Button key={index} data-key={index} style={this.btnStyle}>
{this.store.capitalizeFirstLetter(collection.name)}
</Button>
</Link>
第一次按下按钮时,这工作正常。但是,如果用户尝试通过按下不同的按钮来更改数据,图表组件根本不会刷新,浏览器会显示 URL 已更改,但组件根本不会刷新。
我知道这是因为,我在图表组件中放置了一个 console.log 并且在第二次按下按钮时它没有出现。
componentDidMount = () => {
const { match: { params } } = this.props;
this.topic = params.topic;
console.log("chart topic", this.topic);
this.refreshData(true);
this.forceUpdate();
this.store.nytTest(this.topic, this.startDate, this.endDate);
};
如您所见,我尝试调用 forceUpdate() ,但什么也没做。任何帮助表示赞赏!
解决方案
componentDidMount
仅在安装(渲染)组件时调用一次。您应该使用getDerivedStateFromProps
来更新您的组件
推荐阅读
- maven-surefire-plugin - Concordion 找不到规范
- vb.net - 将数据从 CSV 文件导入数据网格
- javascript - 在带有 event.code 的 switch 语句中使用 JavaScript 运算符
- powershell - 需要一个单行 powershell 命令来下载具有基本身份验证的文件
- selenium - 使用 Selenium 的 Internet Explorer 驱动程序在访问外部网页时不允许处理窗口
- drupal - Drupal 8 第一个 MVC
- ruby-on-rails - Rails - 每当更新对象时进行 api 调用的最佳方式
- arrays - 如何使用 numpy 有效地生成元组数组
- python - 使用 qtbot 在 PySide2 中进行 GUI 测试
- rest - 如何在 Contiki OS 中使用 block2 实现 coap 观察者