首页 > 解决方案 > 在 Aurelia 框架内的 React 中使用 redux

问题描述

我正在为我的客户端应用程序使用 Aurelia 框架。但是,出于某种特殊原因,我在某些屏幕上使用了 react。在客户端应用程序中实现 redux,我发现它非常复杂,因为 redux 已经用于 react 部分。有人知道如何将同一家商店与 react 和 aurelia 一起使用吗?提前致谢。

标签: reactjsreduxaurelia

解决方案


好吧,没有什么能阻止您在 Aurelia 中使用同一家商店。这是我的一篇关于在 Aurelia 中使用 Redux 的博客文章,其中包含如下示例:

import marked from 'marked';
import { bindable } from 'aurelia-framework';
import { createStore } from 'redux';

export class MarkdownRedux {
  @bindable raw;
  html = '';
  store = createStore(textUpdater);

正如您在上面的行中看到的那样,此时它实际上创建了一个新商店。因此,您应该只从 React 获取对先前创建和导出的存储的引用,而不是那样做。

  constructor() {
    this.store.subscribe(this.update.bind(this));
  }

  update() {
    const state = this.store.getState();
    this.html = state.html;
    this.raw = state.raw;
  }

  keyupHandler(newValue) {
    this.store.dispatch(updateText(newValue));
  }

  attached() {
    this.keyupHandler(this.raw);
  }
}

在此之下,所有状态的调度和获取都与往常一样。


推荐阅读