React+Flux数据错误会产生小吃条通知
React + Flux data errors produce snackbar notification?
类似于这个问题,我使用的是material UI和React,我想在数据错误时显示Snackbar通知。
显著的区别是,我是用一种相当普通的Flux方式来做这件事的,而不是Redux
具体来说,如果fetch
操作无法连接到后端服务器,我希望显示一个通知用户的小吃条通知。
在我对Flux的理解中,UI以单向方式发出影响商店的操作。由于错误是在数据存储中异步发生的,我如何将错误冒泡到snackbar?
动作
export default {
loginUser: () => {
AppDispatcher.dispatch({ actionType: LOGIN });
}
}
存储
class LoginStore extends EventEmitter {
constructor() {
super()
this.snackbarError = undefined
}
_registerToActions(action) {
switch(action.actionType) {
case LOGIN:
fetch('/auth').catch((error) => {
this.snackbarError = 'Auth failed!'
})
this.emitChange();
break;
}
}
}
}
组件
class MyComponent extends React.Component {
// how do I trigger snackbar.show() when LoginStore.snackbarError set?
render (
<h3>My Component</h3>
<button onClick={LoginAction.loginUser}>Login</button>
<Snackbar message={LoginStore.snackbarError} />
)
}
类似的东西
class LoginStore extends EventEmitter {
constructor() {
super()
this.snackbarError = undefined
}
_registerToActions(action) {
switch(action.actionType) {
case LOGIN:
fetch('/auth').catch((error) => {
this.snackbarError = 'Auth failed!'
this.emitSomeWhoCallMyComponent();
})
break;
}
}
}
}
和
class MyComponent extends React.Component {
// how do I trigger snackbar.show() when LoginStore.snackbarError set?
componentDidMount()
{
this._doSomething = () => doSomething();
LoginStore.addSomeWhoCallMyComponentListener(this._doSomething);
}
doSomething()
{
snackbar.show();
}
render (
<h3>My Component</h3>
<button onClick={LoginAction.loginUser}>Login</button>
<Snackbar message={LoginStore.snackbarError} />
)
}
相关文章:
- 如何链接 GCM Chrome 推送通知和有效负载数据
- 如何将数据(如用户 ID)传递给 Web 工作人员,以便从服务器获取其他推送通知数据
- 当新数据插入 MySQL 时,AJAX 通知警报弹出或发出声音
- 如何从eBay通知中获取数据
- 使用来自 Web 推送通知的数据
- 使用编码器点火器保存数据后显示通知
- 如何正确地通知聚合物中兄弟姐妹之间的数据变化
- 通知有关数据接收的指令
- Dojo DataGrid如何通知视图服务器上的数据已更改
- React+Flux数据错误会产生小吃条通知
- 如何在用户移动到另一个页面时向用户显示通知而不保存数据
- 如何缓存Ajax调用获得的通知数据
- 如何从请求中检索通知/数据
- 无法加载数据URL为chrome浏览器的GCM推送通知'notificationclick'事件
- FCM消息通知消息和数据消息(WEB桌面推送通知)
- Ajax/php获取最新数据通知
- 带有推送事件数据的推送通知
- 需要从不安全的URL调用数据为chrome的GCM推送通知
- 烬在哪里放置数据的通用代码,如通知,用户配置文件在导航栏上
- 如何根据在 HTML5 中使用服务器发送的事件检索的数据显示通知