【发布时间】:2019-06-14 02:03:01
【问题描述】:
AuthLinks 组件应该在 Notifications 组件的 notificationCount 中渲染来自 notificationCount 属性的值
我想从 notificationCount 获取 AuthLinks 组件的值, 但是变量的值似乎应该在 AuthLinks 上。
const AuthLinks = props => {
return (
<div>
<NavLink to="/notifications">
Notifications
<span data-badge={props.notificationCount} />
</NavLink>
</div>
);
};
import React, { Component } from "react";
import { Link } from "react-router-dom";
class Notifications extends Component {
constructor(props) {
super(props);
this.state = {
notifications: [],
};
}
componentWillMount() {
fetch("/notifications")
.then(response => {
return response.json();
})
.then(data => {
this.setState({ notifications: data });
});
}
render() {
let notificationCount = this.state.notifications.length;
let notifications = this.state.notifications.map(notification => {
return (
<ul>
<li>
<Link to={`/posts/${notification.post.title}`}>
<div>
{notification.post.title}
</div>
</Link>
</li>
</ul>
);
});
return (
<div className="column col-9">
{notificationCount}
{notifications}
</div>
);
}
导出默认通知;
不显示错误消息,但也没有呈现值
【问题讨论】:
-
你在哪里使用你的
AuthLinks组件?
标签: javascript reactjs