【发布时间】:2019-04-24 17:45:48
【问题描述】:
这是我的实际代码:
class Notifications extends React.Component {
constructor(props) {
super(props);
this.state = {
newNotifications: null, //this always must be a null in constructor.
};
}
componentDidUpdate(prevProps, prevState) {
if (this.props.isNewNotification !== prevProps.isNewNotification) {
this.setState({
newNotifications: prevProps.newNotifications,
});
}
}
...
我的 prevProps.newNotifications 是一个数组,例如:
[{"date_time":"Wednesday, 19:42","amount_money":"2,10 USD","sender_name":"John Polaszek"}]
当prevState.newNotifications 不为空时,我想将我的prevProps.newNotifications 数组合并到prevState.newNotifications 中。在render() 我有这个方法:
{newNotifications ? (
<div className="no-test">
{newNotifications.map((newNotification, id) => (
<Fragment>
<span>
{newNotification.sender_name}
</span>
<span className="notification_amount">
{newNotification.amount_money}
</span>
</span>
<br />
<span>
{newNotification.date_time}
</span>
</Fragment>
))}
</div>
)...
我该怎么做?我希望我的问题可以理解。
【问题讨论】:
-
添加的
newNotification不是作为props 的一部分传入的吗?
标签: javascript arrays reactjs