reflux / refluxjs

A simple library for uni-directional dataflow application architecture with React extensions inspired by Flux
BSD 3-Clause "New" or "Revised" License
5.36k stars 330 forks source link

.d.ts文件版本问题 @types\reflux #525

Open chenshuai2144 opened 7 years ago

chenshuai2144 commented 7 years ago

这样写是错误的


class StatusStore extends Reflux.Store {
    page: string
    constructor() {
        super();
        this.listenables = pageAction;
        this.page = location.hash.replace('#', '')
    }

    onPush(page) {
        if (this.page === page) {
            return false;
        }
        LeftMeunAction.hide();
        this.page = page;
        hashHistory.push(page);
    }
}
kriscarle commented 7 years ago

The error appears to be related to TypeScript? I don't think the definitions at https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/reflux have been updated yet for version 6 of reflux.

Also in version 6+ of Reflux you'll probably need to put your values inside the state object. This is how I would updated it:

class StatusStore extends Reflux.Store {

  constructor() {
    super();
    this.listenables = pageAction;
    this.state = {
      page: location.hash.replace('#', ''),
      hashHistory: []
    }
  }

  onPush(newPage) {
    if (this.state.page === page) {
      return false;
    }
    LeftMeunAction.hide();

    var prevPage = this.state.page;
/*
Note: you should also clone hashHistory (treat it as immutable) if you need to detect changes in React components,
for example state.hashHistory.length !== nextState.hashHistory.length in shouldComponentUpdate
JSON.parse(JSON.stringify()) is a hack to clone an object, but there are other ways to do that :)
*/
    var historyClone = JSON.parse(JSON.stringify(this.state.hashHistory));
    historyClone.push(prevPage);
    this.setState({
      page: newPage,
      hashHistory: historyClone
    });
  }
}
cdscawd commented 7 years ago

国际化

LiangZugeng commented 5 years ago

The type definition for Reflux v6.4 has been updated on Jun 26, 2018, it now supports the new class usage of Reflux.