Inhaltsverzeichnis
回复内容:
Heim Web-Frontend H5-Tutorial 比较redux和reflux以及自己写个TinyFlux?

比较redux和reflux以及自己写个TinyFlux?

Jun 07, 2016 am 08:44 AM
redux store view

看了redux和reflux,感觉redux好复杂啊,看reflux,1个小时就能弄懂,可是看redux,1天下来都迷迷糊糊的。

感觉flux不就应该是view层发消息给store,store根据消息去修改数据,修改完数据后通知涉及到被修改的view组建刷新state吗。。。

然后我看着reflux的使用方式(它的源代码没怎么看,感觉还是多了),自己写了个tinyFlux。。。二楼附上。。各位看看。。。。可能还太幼稚了。。。欢迎批评啊。。。

回复内容:

谢邀,Flux 相关的轮子已经太多了,实在看不过来,抱歉。

简单说说 Reflux 和 Redux 的区别吧。

之前我在 如何理解 Facebook 的 flux 应用架构? - 知乎用户的回答 里提到过,目前市面上很多 Flux 相关的框架就是把 Dispatcher 隐藏了,然后封装了 Action Creator 和 Component 绑定的细节,简单地说就是「简化 Flux 里冗余的代码和操作」。

Reflux 在实现上面说到的内容的基础上,又提供了很多开发者喜欢的功能,比如 View 监听 Action 这种违背 Flux 概念但是确实在实际应用中很方便的特性。

而 Redux 并不仅仅是「简化 Flux」,它重新定义了 Flux 中每一个角色的功能、职责及实现方式。其中最大的不同是没有 Store 的概念(或者说 Store 不再需要用户去定义),而增加了 Reducer 的概念。Store 会通过我们定义的 Reducer 自动生成,使用 redux.createStore 方法。此外,Redux 定义了一个 middleware 机制,可以让我们在 Action 中更方便的处理业务逻辑。

总结一下就是,如果你觉得目前 Flux 用着不爽太多冗余代码, 那么你写的 TinyFlux 很不错,解决了你的很多问题;然而如果你想拥有更多 redux 带来的新特性,或者说你喜欢并推崇不可变的数据结构,或者说你想显得逼格比较高,那么看懂并会用 redux 绝对是你的不二选择。

PS. redux 确实不错,我们已经开始着手在生产环境中使用了。 Redux 核心概念 深入浅出Redux w3ctech.com/topic/1561
<span class="c1">//TinyFlux.js</span>
<span class="kd">function</span> <span class="nx">createStore</span> <span class="p">(</span><span class="nx">store</span><span class="p">,</span> <span class="p">...</span><span class="nx">args</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">store</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'请定义store'</span><span class="p">);</span>
  <span class="p">};</span>
  <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">store</span><span class="p">.</span><span class="nx">init</span> <span class="o">===</span> <span class="s1">'function'</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">store</span><span class="p">.</span><span class="nx">init</span><span class="p">(</span><span class="nx">args</span><span class="p">);</span>
  <span class="p">};</span>
  <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">store</span><span class="p">.</span><span class="nx">actions</span> <span class="o">!==</span> <span class="s1">'function'</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'actions应该是一个返回一个对象的function,对象里的方法都要用箭头表达式来书写,利用箭头表达式的this绑定。'</span><span class="p">);</span>
  <span class="p">};</span>
  <span class="nx">store</span><span class="p">.</span><span class="nx">actions</span> <span class="o">=</span> <span class="nx">store</span><span class="p">.</span><span class="nx">actions</span><span class="p">();</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">store</span><span class="p">.</span><span class="nx">subscriber</span> <span class="o">||</span> <span class="nx">store</span><span class="p">.</span><span class="nx">emit</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'不该存在subscriber和emit'</span><span class="p">);</span>
  <span class="p">};</span>
  <span class="nx">store</span><span class="p">.</span><span class="nx">subscriber</span> <span class="o">=</span> <span class="p">[];</span>
  <span class="nx">store</span><span class="p">.</span><span class="nx">emit</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">argument</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">store</span><span class="p">.</span><span class="nx">subscriber</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">component</span> <span class="o">=></span> <span class="nx">component</span><span class="p">.</span><span class="nx">reload</span><span class="p">());</span>
  <span class="p">};</span>
  <span class="k">return</span> <span class="nx">store</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">connect</span> <span class="p">(</span><span class="nx">store</span><span class="p">,</span> <span class="nx">def</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="p">{</span>
      <span class="nx">getInitialState</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
        <span class="kd">var</span> <span class="nx">state</span> <span class="o">=</span> <span class="p">{};</span>
        <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">def</span><span class="p">)</span> <span class="p">{</span>
          <span class="k">try</span><span class="p">{</span>
            <span class="nx">state</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="s1">'store.'</span><span class="o">+</span><span class="nx">def</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
          <span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
          <span class="p">}</span>
        <span class="p">};</span>
        <span class="k">return</span> <span class="nx">state</span><span class="p">;</span>
      <span class="p">},</span>
      <span class="nx">reload</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">state</span> <span class="o">=</span> <span class="p">{};</span>
        <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">def</span><span class="p">)</span> <span class="p">{</span>
          <span class="k">try</span><span class="p">{</span>
            <span class="nx">state</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="s1">'store.'</span><span class="o">+</span><span class="nx">def</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
          <span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
          <span class="p">}</span>
        <span class="p">};</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">setState</span><span class="p">(</span><span class="nx">state</span><span class="p">);</span>
      <span class="p">},</span>
      <span class="nx">componentDidMount</span><span class="o">:</span> <span class="kd">function</span><span class="p">(){</span>
        <span class="nx">store</span><span class="p">.</span><span class="nx">subscriber</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
      <span class="p">},</span>
      <span class="nx">componentWillUnmount</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="nx">store</span><span class="p">.</span><span class="nx">subscriber</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">store</span><span class="p">.</span><span class="nx">subscriber</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="k">this</span><span class="p">),</span><span class="mi">1</span><span class="p">);</span>
      <span class="p">}</span>
  <span class="p">};</span>
<span class="p">}</span>

<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">createStore</span><span class="o">:</span> <span class="nx">createStore</span><span class="p">,</span>
  <span class="nx">connect</span><span class="o">:</span> <span class="nx">connect</span>
<span class="p">}</span>
Nach dem Login kopieren
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Crossplay haben?
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Laravel-Entwicklung: Wie generiert man Ansichten mit Laravel View? Laravel-Entwicklung: Wie generiert man Ansichten mit Laravel View? Jun 14, 2023 pm 03:28 PM

Laravel ist derzeit eines der beliebtesten PHP-Frameworks und seine leistungsstarken Funktionen zur Ansichtsgenerierung sind beeindruckend. Eine Ansicht ist eine Seite oder ein visuelles Element, das dem Benutzer in einer Webanwendung angezeigt wird und Code wie HTML, CSS und JavaScript enthält. Mit LaravelView können Entwickler eine strukturierte Vorlagensprache verwenden, um Webseiten zu erstellen und entsprechende Ansichten über Controller und Routing zu generieren. In diesem Artikel erfahren Sie, wie Sie mit LaravelView Ansichten generieren. 1. Was

Der Microsoft Store lässt sich nicht öffnen und zeigt „Entschuldigung! Etwas ist schiefgelaufen, aber wir haben es richtig gemacht' – [Detaillierte Lösung] Der Microsoft Store lässt sich nicht öffnen und zeigt „Entschuldigung! Etwas ist schiefgelaufen, aber wir haben es richtig gemacht' – [Detaillierte Lösung] Mar 27, 2024 pm 01:21 PM

Einige Benutzer möchten ihre Lieblings-Apps im Microsoft Store finden, herunterladen und installieren, stellen jedoch fest, dass der Microsoft Store nicht geöffnet werden kann und außerdem die Meldung „Entschuldigung! Etwas ist schief gelaufen, aber wir haben es richtig gemacht“ angezeigt wird Lösen Sie das Problem, damit es geöffnet werden kann. Ist der Microsoft Store wieder betriebsbereit? Der Herausgeber hat unten zwei Methoden zusammengestellt. Ich hoffe, sie können Ihnen helfen! Methode eins: Drücken Sie Win+R→cmd eingeben und halten Sie dann Strg+Umschalt gedrückt→klicken Sie auf „OK“ (klicken Sie auf „Ja“, nachdem die Benutzerkontensteuerung angezeigt wird) und dann erscheint das cmd-Fenster (Administratormodus) und kopieren Sie dann den folgenden Inhalt und fügen Sie ihn ein: netshwinsockresetnetshintipresetipconfig/ releaseipconfig/renewi

React Redux Tutorial: So verwenden Sie Redux zur Verwaltung des Front-End-Status React Redux Tutorial: So verwenden Sie Redux zur Verwaltung des Front-End-Status Sep 26, 2023 am 11:33 AM

ReactRedux-Tutorial: So verwalten Sie den Front-End-Status mit Redux React ist eine sehr beliebte JavaScript-Bibliothek zum Erstellen von Benutzeroberflächen. Und Redux ist eine JavaScript-Bibliothek zur Verwaltung des Anwendungsstatus. Gemeinsam helfen sie uns, den Front-End-Status besser zu verwalten. In diesem Artikel wird erläutert, wie Sie mit Redux den Status in React-Anwendungen verwalten, und es werden spezifische Codebeispiele bereitgestellt. 1. Redux installieren und einrichten Zuerst müssen wir Re installieren

Ein Artikel, der die Details der Verwendung von Redux Hooks erläutert Ein Artikel, der die Details der Verwendung von Redux Hooks erläutert Nov 11, 2022 pm 06:24 PM

In diesem Artikel geht es um die Details der Verwendung von Redux Hooks. Ich hoffe, dass er Ihnen hilfreich sein wird!

So lösen Sie das Responsive-Value-Problem des Vue3- und Vuex4-Stores So lösen Sie das Responsive-Value-Problem des Vue3- und Vuex4-Stores May 29, 2023 pm 06:55 PM

Szenario: Wenn auf der Seite auf eine Schaltfläche geklickt wird, erhöht sich die Menge und der Wert wird im Geschäft gespeichert. Wenn auf die Schaltfläche geklickt wird, ändert sich der Wert nicht. import{useStore}from'@/vuex';conststore=useStore()constonSubmit=()=>{store.dispatch("incrementAction",1);}letcount=store.state.count{{count}}Grund: speichern .state.count ist die falsche Wertmethode. Sie kann zwar entfernt werden, die Antwort geht jedoch verloren.

Best Practices zum Erstellen skalierbarer Webanwendungen mit Go und Redux Best Practices zum Erstellen skalierbarer Webanwendungen mit Go und Redux Jun 18, 2023 pm 01:59 PM

Mit der Weiterentwicklung von Webanwendungen beginnen immer mehr Unternehmen, vom traditionellen Back-End-Rendering zum Front-End-Rendering überzugehen. Um dieses Ziel zu erreichen, übernehmen viele Unternehmen die Go-Sprache und Redux in Webanwendungen. In diesem Artikel besprechen wir die Best Practices für die Erstellung skalierbarer Webanwendungen mithilfe dieser beiden Technologien. Vertraut mit der Go-Sprache Go-Sprache ist eine von Google entwickelte Open-Source-Programmiersprache. Es ist einfach, effizient und sicher und kann zum Erstellen hochgradig gleichzeitiger Webanwendungen verwendet werden. Gebaut mit der Go-Sprache

So implementieren Sie mithilfe von Reflektion und dynamischem Proxy eine View-Annotationsbindungsbibliothek in Java So implementieren Sie mithilfe von Reflektion und dynamischem Proxy eine View-Annotationsbindungsbibliothek in Java Apr 30, 2023 pm 05:34 PM

Verwenden Sie Reflection in Kombination mit dynamischem Proxy, um eine View-Annotationsbindungsbibliothek zu implementieren, die View- und Event-Bindung unterstützt. Der Code ist prägnant, einfach zu verwenden und weist eine starke Skalierbarkeit auf. Unterstützte Funktionen @ContentView bindet Layout anstelle von setContentView() @BindView bindet View anstelle von findViewById() @OnClick bindet Click-Ereignis anstelle von setOnClickListener() @OnLongClick bindet Long-Press-Ereignis anstelle von setOnLongClickListener() Codeanmerkungsklasse @Target(ElementType. TYPE )@Retention(Rete

So implementieren Sie mit React und Redux ein komplexes Front-End-Datenmanagement So implementieren Sie mit React und Redux ein komplexes Front-End-Datenmanagement Sep 26, 2023 am 09:30 AM

So implementieren Sie mit React und Redux ein komplexes Front-End-Datenmanagement. Vorwort: Mit der kontinuierlichen Weiterentwicklung der Front-End-Technologie werden Front-End-Anwendungen immer komplexer und das Datenmanagement wird immer wichtiger. React und Redux sind derzeit beliebte Front-End-Frameworks und können uns effektiv bei der Verwaltung und Aktualisierung von Daten helfen. In diesem Artikel wird erläutert, wie Sie mit React und Redux ein komplexes Front-End-Datenmanagement implementieren, und es werden spezifische Codebeispiele bereitgestellt. 1. Einführung in React React ist ein JavaScript, das zum Erstellen von Benutzeroberflächen verwendet wird

See all articles