当请求新数据的操作进入时,您可以理智地获得最接近无缓存的方法是将任何存储状态重置为 null 或 []。如果这样做,您必须发出更改事件,否则您会引发竞争条件。
作为 Flux 的替代方案,您可以简单地使用 Promise 和带有 api 的简单 mixin 来修改状态。例如,蓝鸟:
var promiseStateMixin = {
thenSetState: function(updates, initialUpdates){
// promisify setState
var setState = this.setState.bind(this);
var setStateP = function(changes){
return new Promise(function(resolve){
setState(changes, resolve);
});
};
// if we have initial updates, apply them and ensure the state change happens
return Promise.resolve(initialUpdates ? setStateP(initialUpdates) : null)
// wait for our main updates to resolve
.then(Promise.params(updates))
// apply our unwrapped updates
.then(function(updates){
return setStateP(updates);
}).bind(this);
}
};
在您的组件中:
handleRefreshClick: function(){
this.thenSetState(
// users is Promise<User[]>
{users: Api.Users.getAll(), loading: false},
// we can't do our own setState due to unlikely race conditions
// instead we supply our own here, but don't worry, the
// getAll request is already running
// this argument is optional
{users: [], loading: true}
).catch(function(error){
// the rejection reason for our getUsers promise
// `this` is our component instance here
error.users
});
}
当然,这并不妨碍您在应用程序中有意义的时间/地点使用助焊剂。例如,react-router 被用于许多 react 项目中,并且它在内部使用了通量。React 和相关库/模式旨在仅在需要时提供帮助,而从不控制您编写每个组件的方式。