使用Reom和pusher的Isomorphic框架来获取websockets。
当我进入componentDidMount()
函数时,我似乎无法访问状态。
class TopbarNotification extends Component {
state = {
visible: false,
notifications: []
};
constructor(props) {
super(props);
this.state = {
notifications: [],
visible: false
};
}
componentDidMount() {
var pusher = new Pusher('xxxxxxxxxxxx', {
cluster: 'xxx',
encrypted: true
});
var channel = pusher.subscribe('notifications');
channel.bind('new-notification', function (data) {
let newNotifications = this.state.notifications.push(data)
this.setState({
notifications: newNotifications
})
alert(data.message);
});
}
render() {
// ...Blah blah, blah blah
}
}
我收到此错误:TypeError: Cannot read property 'notifications' of undefined
参考此行:let newNotifications = this.state.notifications.push(data)
为什么我无法访问state
内的componentDidMount()
?