Javascript 访问组件中动作创建者返回的信息是否会中断流量?
在组件中执行操作时,访问组件中的操作创建者返回的承诺是否会打破通量模式 动作创作者Javascript 访问组件中动作创建者返回的信息是否会中断流量?,javascript,reactjs,flux,Javascript,Reactjs,Flux,在组件中执行操作时,访问组件中的操作创建者返回的承诺是否会打破通量模式 动作创作者 class MyActionCreators extends Marty.ActionCreators { create() { this.dispatch(MyActionConstants.CREATE_ACTION); return MyActionAPI.create() .then(res) { this.dispatch(MyActionConstan
class MyActionCreators extends Marty.ActionCreators {
create() {
this.dispatch(MyActionConstants.CREATE_ACTION);
return MyActionAPI.create()
.then(res) {
this.dispatch(MyActionConstants.CREATE_ACTION_DONE);
return res;
};
}
}
成分
class MyCompoment extends React.Component {
render() {
return(
<div>
<button onChange={ this._clickAction }>Create Me!<button>
</div>
);
}
// private
_clickAction() {
MyActionCreators.create()
// Does this break Flux?
.then((res) => {
this.context.router.transitionTo('some_path', { id: res.id });
});
}
}
类myComponent扩展了React.Component{
render(){
返回(
创造我!
);
}
//私人的
_单击操作(){
MyActionCreators.create()
//这会打破通量吗?
。然后((res)=>{
this.context.router.transitiono('some_path',{id:res.id});
});
}
}
商店是否是访问上述示例中所需信息的唯一适当位置?答案
这确实会中断流量,因为它将状态从存储区移开,并使操作直接与视图通信
可能的解决办法 在您的系统上做一些假设,我将假设您有一个正在尝试更新的路由器 注意:示例并不完整,它们只是给出了基本概念。它们也不基于Flux架构的任何实现。 商店 创建一个将包含id的存储。该存储必须发出某种更改事件 示例
var Store = function() {
this._id;
Dispatcher.on(MyActionConstants.CREATE_ACTION_DONE, function(payload) {
this._id = payload.id;
this.emit("change");
}.bind(this));
}
Store.prototype.getId = function() {
return this._id;
}
var store = new Store();
store.on('change', function() {
router.transitionTo('some_path', { id: store.getId() })
});
控制器视图
创建一个控制器视图(类似于react组件),该视图将根据存储中的更改更新路由器
示例
var Store = function() {
this._id;
Dispatcher.on(MyActionConstants.CREATE_ACTION_DONE, function(payload) {
this._id = payload.id;
this.emit("change");
}.bind(this));
}
Store.prototype.getId = function() {
return this._id;
}
var store = new Store();
store.on('change', function() {
router.transitionTo('some_path', { id: store.getId() })
});