React Flux 库 Alt 是否使用 React 的状态?

Does React Flux library Alt use React's state?

完成 guide on the Alt site I am confused if Alt uses Reacts state to set properties? In part 3 - 使用商店后,显示

Instance variables defined anywhere in the store will become the state.

然后它在存储中放入一个变量:

this.locations = [];

然而,如果我进入主要的 React 组件并记录状态,当然在给定位置数据之后,我得到 undefined 除了道具?

loggState() {
  console.log(this.state); // undefined
  console.log(this.locations); // undefined
  console.log(this.props.locations); // [object][object]..
}

谁能解释一下使用Alt时状态和道具之间的关系?

在 Alt 中——实际上是大多数 Flux 实现——store 是应用程序中与组件完全不同的部分。

组件 订阅 商店中的更改,然后使用更改的数据更新其本地状态,使其重新呈现。

当我们实例化组件时,无论商店的状态如何,我们都会从使用 LocationStore 的组件中导出初始状态。

getInitialState() {
  return LocationStore.getState();
},

然后我们设置一个订阅,监听商店的变化。

componentDidMount() {
  LocationStore.listen(this.onChange);
},

最后,我们使用订阅处理程序将这些更改应用到组件的 本地状态。您决定应用每个更新的方式完全取决于您。

onChange(state) {
  this.setState(state);
},

每次我们用新状态调用 this.setState 时,组件都会重新渲染。

您也可以使用 higher-order component 订阅商店,然后将商店的状态转换为 props 并将它们传递给包装的组件。在这种情况下,您的组件根本不需要是有状态的。