reactjs - 如何根据 redux 数据在通用 Header 组件中添加类
问题描述
在我的 react JS 应用程序中,我在标题组件中添加了一个通知图标。我创建了一个单独的组件,我在其中进行 api 调用以获取数据并显示它。如果有一些通知警报,我在这里想要实现的是更改 Header 组件中图标的颜色。我的 Header 组件-
import React from "react";
import { connect } from "react-redux";
import {
setPoiData,
getNotification,
updateNotification
} from "../../actions/action";
import { Link } from "react-router-dom";
const axios = require("axios");
class Notification extends React.Component {
render() {
const data = this.props.getNotificationStatus;
const highlightBellIcon = Object.keys((data.length === 0))
return (
<div className="notification-parent">
<Link to="/notification-details">
<span className={"glyphicon glyphicon-bell " + (!highlightBellIcon ? 'classA' : 'classB')} />
</Link>
</div>
);
}
}
const mapStateToProps = state => ({
getNotificationStatus: state.root.getNotificationStatus
});
export default connect (mapStateToProps)(Notification)
这里,getNotificationStatus 是 Redux 中保存值的状态。
通知详细信息组件-
import React from "react";
import { connect } from "react-redux";
import {
getNotification
} from "../../actions/action";
import { Spinner } from "../Spinner";
import { setTimeout } from "timers";
import NotificationTile from "../NotificationTile/NotificationTile";
const axios = require("axios");
class NotificationDetails extends React.Component {
componentDidMount = () => {
this.intervalId = setInterval(() => this.handleNotification(), 2000);
setTimeout(
() =>
this.setState({
loading: false
}),
10000
);
};
componentWillUnmount = () => {
clearInterval(this.intervalId);
};
handleNotification = () => {
let postData = {
//inputParams
}
//call to action
this.props.dispatch(getNotification(postData));
};
getNotificationDetails = data => {
const payloadData =
data.payLoad &&
data.payLoad.map(item => {
console.log(this);
return <NotificationTile {...item} history={this.props.history} />;
});
//console.log(payloadData);
return payloadData;
console.log("InitialState" + payloadData);
};
render() {
const { loading } = this.state;
const data = this.props.getNotificationStatus;
return (
<div className="notificationContainer container">
<div className="notification-alert">
{!loading ? (
this.getNotificationDetails(data)
) : (
<h1>
Waiting for notifications..
<Spinner />
</h1>
)}
</div>
</div>
);
}
}
const mapStateToProps = state => ({
getNotificationStatus: state.root.getNotificationStatus
});
export default connect(mapStateToProps)(NotificationDetails);
我面临的问题始终是 classB 被添加,因为 api 调用发生在单击铃铛图标时。所以当我第一次登陆页面时,除非我点击铃铛图标,否则不会发生 api 调用。我的代码绝对可以正常工作,只是我需要根据在作为兄弟组件的 NotificationDetail Comp 中收到的响应将类添加到我的 Notification 组件(这是一个全局组件)中。我哪里出错了有什么建议吗?
解决方案
notification-details
是一个单独的组件,它获取数据并将其添加到存储中, <Link to="/notification-details">
并且加载该组件,在这种情况下,您的存储最初不会有数据,直到您单击铃铛图标,这是正确的行为。
解决方案:
请在你的组件树中上一层,我的意思是去一个优于 Notification 的组件(它的 Container 组件),并在那里加载通知细节(你可以使用任何创建生命周期钩子),还有一个标志(isNotificationLoaded 之类的东西,它检查state.root.getNotificationStatus 填充数据的位置,并仅在其为真后加载通知面板。
就像是
render() {
const data = this.props.getNotificationStatus;
const noticationPanel = data.length > 0 ? <Notification/> : null;
return({noticationPanel});
}
这将确保仅在有数据时加载,在此之前您可以显示一些活动指示器。
希望这有助于解决您的问题。
推荐阅读
- html - 为什么复选框字段后的这个字段总是浮动在右边?
- android - 小部件在 Android 8.0 中不更新
- selenium-webdriver - 无法在硒中找到下拉菜单
- jquery - Boostrap 日期选择器日期为各种选项打开
- android - 如何在 android 中打开导航抽屉,如下图所示
- angular - 如何创建一个通用服务以集成所有页面的 jQuery 数据表 Angular
- javascript - 如何在 Jquerydatatable 中获取大数据
- find - find(1) 列出文件的顺序是什么?
- amazon-web-services - Dynamo 数据库读/写
- qt - QML ComboBox 弹出问题