reactjs - 如何在 React 中重新定义组件?
问题描述
react/redux 有一个水疗中心。品牌有一个组成部分。Media 组件与它类似,但数据是从另一个 Ajax 请求中检索的。我正在尝试从 Brands 继承,但由于某种原因,componentDidMount 是从 Brands 启动的。怎么做才对?
class Media extends Brands {
loadData = async () => {
alert('sdfdf'); //not displayed
const { dispatch, dataList, clientId } = this.props;
if (!dataList && clientId) {
dispatch(mainLoaderActions.addBlockingRequest("loadMedia"));
const res = await mediaApi.getBrandsData(clientId);
dispatch(dataActions.setBrandsList(res));
dispatch(mainLoaderActions.deleteBlockingRequest("loadMedia"));
}
};
componentDidMount() {
alert('aaa'); //not displayed
this.loadData();
}
render() {...
解决方案
React 不支持类继承,因此很困难!请参阅此处的文档:https ://reactjs.org/docs/composition-vs-inheritance.html这里有一个类似问题的答案:componentDidMount method not trigger when using inherited ES6 react class with an example of 'react' way of实现这一目标。
推荐阅读
- qt - 如何在 qml 中实现嵌套列表模型
- c++ - 无法激活蜥蜴分析器 [vscode]
- python - 错误的函数 Python
- xamarin.forms - Xamarin 的表单 Date Picker android 图标有点偏
- xamarin.forms - 我应该使用哪个包在 xamarin-forms 中嵌入 admob?
- docker - Docker compose 无法绑定到任何端口
- contiki - Contiki 中发送和接收的数据包总数
- swift - Xcode 13.0 不自动补全功能/代码
- sql - SQL Using LIKE and ANY at the same time
- html - 使用 Selenium 在 html 中查找优质元素