react本地通量同步调度错误

react native flux synchronous dispatch error

本文关键字:同步 调度 错误 react      更新时间:2023-09-26

我正在使用包alt构建一个应用程序,可以在这里找到:https://github.com/goatslacker/alt

我在注销操作中遇到了一个错误,因为我认为是同步调度,我不知道如何使用flux的waitFor调用。。

这是我的代码:

使用注销调用查看:

import ProfilStore from '../../stores/Profil';
class Profil extends Page {
static route(props, title) {
  return {
    component: Profil,
    title: title || 'Profil',
    passProps: props
  };
}
currentProfil() {
  return this.props.id || MeStore.getState().me.id;
}
getProfilState() {  
  return {
    data: ProfilStore.getState().profils[this.currentProfil()],
    loading: ProfilStore.loading(this.currentProfil())
  };
}
constructor(props) {
  super(props);
  this.state = {
    errors: []
  };
  this.state = this.getProfilState();
}
onFocus = (event) => {
  if (event.data.route.component === Profil) {
    ProfilActions.fetchProfil(this.currentProfil());
  }
}
componentWillMount() {
  ProfilStore.listen(this.onProfilsChange);
  // it can be a tab view or a pushed view
  if (!this.props.id) {
    this.props.navigator.navigationContext.addListener('didfocus', this.onFocus);
  } else {
    ProfilActions.fetchProfil(this.currentProfil());
  }
}
componentWillUnmount() {
  ProfilStore.unlisten(this.onProfilsChange);
}
onProfilsChange = () => {
  this.setState(this.getProfilState());
}
render() {
  return(
    <TouchableHighlight style={styles.logoutbutton} onPress={LoginActions.logout}>
      <Text>Logout</Text>
    </TouchableHighlight>
  );
}
}

LoginAtions.js:

'use strict';
import alt from '../alt';
import LoginUtils from '../utils/login';
export class LoginActions {
  loginSuccess(me) {
    this.dispatch(me);
  }
  loginFailed(err) {
    this.dispatch(err);
  }
  logout() {
    this.dispatch();
  }
  login() {
    this.dispatch();
    LoginUtils.facebook((err, me) => {
      if (err) {
        return this.actions.loginFailed(err);
      }
      this.actions.loginSuccess(me);
    });
  }
}
export default alt.createActions(LoginActions);

ProfileStore.js:

import alt from '../alt';
import _ from 'lodash';
import ProfilActions from '../actions/ProfilActions';
export class ProfilStore extends CachedStore {
constructor() {
  super();
  this.profils = {};
  this.status.profilsLoading = [];
  this.status.profilsLoadingError = {};
  this.status.profilDisplaying = [];
  this.status.profilDisplayingError = {};
  this.bindListeners({
    handleFetchProfil: ProfilActions.FETCH_PROFIL,
    handleProfilFetched: ProfilActions.PROFIL_FETCHED,
    handleProfilFetchFailed: ProfilActions.PROFIL_FETCH_FAILED,
    handleDisplayProfil: ProfilActions.DISPLAY_PROFIL,
    handleDisplayProfilFailed: ProfilActions.DISPLAY_PROFIL_FAILED,
    handleDisplayProfilSuccess: ProfilActions.DISPLAY_PROFIL_SUCCESS
  });
}
handleDisplayProfil(id) {
  this.status.profilDisplaying.push(id);
  delete this.status.profilDisplayingError[id];
}
handleDisplayProfilFailed(data) {
  _.remove(this.status.profilDisplaying, function(id) {
    return id === data.id;
  });
  this.status.profilDisplayingError[data.id] = data.err;
}
handleDisplayProfilSuccess(idProfil) {
  _.remove(this.status.profilDisplaying, function(id) {
    return id === idProfil;
  });
  this.profils[idProfil] = _.extend({}, this.profils[idProfil], {invisible: false});
}
static displayProfilError(id) {
  return this.getState().status.profilDisplayingError[id];
}
static displayProfilLoading(id) {
  return _.contains(this.getState().status.profilDisplaying, id);
}
handleFetchProfil(id) {
  this.status.profilsLoading.push(id);
  delete this.status.profilsLoadingError[id];
}
handleProfilFetched(profil) {
  this.profils[profil.id] = profil;
  _.remove(this.status.profilsLoading, function(id) {
    return id === profil.id;
  });
}
handleProfilFetchFailed(data) {
  _.remove(this.status.profilsLoading, function(id) {
    return id === data.id;
  });
  this.status.profilsLoadingError[data.id] = data.err;
}
static error(id) {
  return this.getState().status.profilsLoadingError[id];
}
static loading(id) {
  return _.includes(this.getState().status.profilsLoading, id);
}
static profil(id) {
  return this.getState().profils[id];
}
}
export default alt.createStore(ProfilStore);

谢谢!

Dirty hack可能会在调用操作调度器的地方包装一个setTimeout(fn,0)。