forked from reduxjs/redux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
961 lines (544 loc) · 43.5 KB
/
Copy pathindex.html
File metadata and controls
961 lines (544 loc) · 43.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Read Me | Redux</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="Redux is a predictable state container for JavaScript apps. ">
<meta name="generator" content="GitBook 2.4.3">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
<link rel="next" href="./docs/introduction/index.html" />
</head>
<body>
<div class="book" data-level="0" data-basepath="." data-revision="Mon Feb 22 2016 14:06:38 GMT+0000 (UTC)">
<div class="book-summary">
<div class="book-search" role="search">
<input type="text" placeholder="Type to search" class="form-control" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter active" data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Read Me
</a>
</li>
<li class="chapter " data-level="1" data-path="docs/introduction/index.html">
<a href="./docs/introduction/index.html">
<i class="fa fa-check"></i>
<b>1.</b>
Introduction
</a>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="docs/introduction/Motivation.html">
<a href="./docs/introduction/Motivation.html">
<i class="fa fa-check"></i>
<b>1.1.</b>
Motivation
</a>
</li>
<li class="chapter " data-level="1.2" data-path="docs/introduction/ThreePrinciples.html">
<a href="./docs/introduction/ThreePrinciples.html">
<i class="fa fa-check"></i>
<b>1.2.</b>
Three Principles
</a>
</li>
<li class="chapter " data-level="1.3" data-path="docs/introduction/PriorArt.html">
<a href="./docs/introduction/PriorArt.html">
<i class="fa fa-check"></i>
<b>1.3.</b>
Prior Art
</a>
</li>
<li class="chapter " data-level="1.4" data-path="docs/introduction/Ecosystem.html">
<a href="./docs/introduction/Ecosystem.html">
<i class="fa fa-check"></i>
<b>1.4.</b>
Ecosystem
</a>
</li>
<li class="chapter " data-level="1.5" data-path="docs/introduction/Examples.html">
<a href="./docs/introduction/Examples.html">
<i class="fa fa-check"></i>
<b>1.5.</b>
Examples
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="docs/basics/index.html">
<a href="./docs/basics/index.html">
<i class="fa fa-check"></i>
<b>2.</b>
Basics
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="docs/basics/Actions.html">
<a href="./docs/basics/Actions.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Actions
</a>
</li>
<li class="chapter " data-level="2.2" data-path="docs/basics/Reducers.html">
<a href="./docs/basics/Reducers.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Reducers
</a>
</li>
<li class="chapter " data-level="2.3" data-path="docs/basics/Store.html">
<a href="./docs/basics/Store.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Store
</a>
</li>
<li class="chapter " data-level="2.4" data-path="docs/basics/DataFlow.html">
<a href="./docs/basics/DataFlow.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Data Flow
</a>
</li>
<li class="chapter " data-level="2.5" data-path="docs/basics/UsageWithReact.html">
<a href="./docs/basics/UsageWithReact.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
Usage with React
</a>
</li>
<li class="chapter " data-level="2.6" data-path="docs/basics/ExampleTodoList.html">
<a href="./docs/basics/ExampleTodoList.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
Example: Todo List
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="docs/advanced/index.html">
<a href="./docs/advanced/index.html">
<i class="fa fa-check"></i>
<b>3.</b>
Advanced
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="docs/advanced/AsyncActions.html">
<a href="./docs/advanced/AsyncActions.html">
<i class="fa fa-check"></i>
<b>3.1.</b>
Async Actions
</a>
</li>
<li class="chapter " data-level="3.2" data-path="docs/advanced/AsyncFlow.html">
<a href="./docs/advanced/AsyncFlow.html">
<i class="fa fa-check"></i>
<b>3.2.</b>
Async Flow
</a>
</li>
<li class="chapter " data-level="3.3" data-path="docs/advanced/Middleware.html">
<a href="./docs/advanced/Middleware.html">
<i class="fa fa-check"></i>
<b>3.3.</b>
Middleware
</a>
</li>
<li class="chapter " data-level="3.4" >
<span><b>3.4.</b> Usage with React Router</span>
</li>
<li class="chapter " data-level="3.5" data-path="docs/advanced/ExampleRedditAPI.html">
<a href="./docs/advanced/ExampleRedditAPI.html">
<i class="fa fa-check"></i>
<b>3.5.</b>
Example: Reddit API
</a>
</li>
<li class="chapter " data-level="3.6" >
<span><b>3.6.</b> Next Steps</span>
</li>
</ul>
</li>
<li class="chapter " data-level="4" data-path="docs/recipes/index.html">
<a href="./docs/recipes/index.html">
<i class="fa fa-check"></i>
<b>4.</b>
Recipes
</a>
<ul class="articles">
<li class="chapter " data-level="4.1" data-path="docs/recipes/MigratingToRedux.html">
<a href="./docs/recipes/MigratingToRedux.html">
<i class="fa fa-check"></i>
<b>4.1.</b>
Migrating to Redux
</a>
</li>
<li class="chapter " data-level="4.2" data-path="docs/recipes/UsingObjectSpreadOperator.html">
<a href="./docs/recipes/UsingObjectSpreadOperator.html">
<i class="fa fa-check"></i>
<b>4.2.</b>
Using Object Spread Operator
</a>
</li>
<li class="chapter " data-level="4.3" data-path="docs/recipes/ReducingBoilerplate.html">
<a href="./docs/recipes/ReducingBoilerplate.html">
<i class="fa fa-check"></i>
<b>4.3.</b>
Reducing Boilerplate
</a>
</li>
<li class="chapter " data-level="4.4" data-path="docs/recipes/ServerRendering.html">
<a href="./docs/recipes/ServerRendering.html">
<i class="fa fa-check"></i>
<b>4.4.</b>
Server Rendering
</a>
</li>
<li class="chapter " data-level="4.5" data-path="docs/recipes/WritingTests.html">
<a href="./docs/recipes/WritingTests.html">
<i class="fa fa-check"></i>
<b>4.5.</b>
Writing Tests
</a>
</li>
<li class="chapter " data-level="4.6" data-path="docs/recipes/ComputingDerivedData.html">
<a href="./docs/recipes/ComputingDerivedData.html">
<i class="fa fa-check"></i>
<b>4.6.</b>
Computing Derived Data
</a>
</li>
<li class="chapter " data-level="4.7" data-path="docs/recipes/ImplementingUndoHistory.html">
<a href="./docs/recipes/ImplementingUndoHistory.html">
<i class="fa fa-check"></i>
<b>4.7.</b>
Implementing Undo History
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5" data-path="docs/Troubleshooting.html">
<a href="./docs/Troubleshooting.html">
<i class="fa fa-check"></i>
<b>5.</b>
Troubleshooting
</a>
</li>
<li class="chapter " data-level="6" data-path="docs/Glossary.html">
<a href="./docs/Glossary.html">
<i class="fa fa-check"></i>
<b>6.</b>
Glossary
</a>
</li>
<li class="chapter " data-level="7" data-path="docs/api/index.html">
<a href="./docs/api/index.html">
<i class="fa fa-check"></i>
<b>7.</b>
API Reference
</a>
<ul class="articles">
<li class="chapter " data-level="7.1" data-path="docs/api/createStore.html">
<a href="./docs/api/createStore.html">
<i class="fa fa-check"></i>
<b>7.1.</b>
createStore
</a>
</li>
<li class="chapter " data-level="7.2" data-path="docs/api/Store.html">
<a href="./docs/api/Store.html">
<i class="fa fa-check"></i>
<b>7.2.</b>
Store
</a>
</li>
<li class="chapter " data-level="7.3" data-path="docs/api/combineReducers.html">
<a href="./docs/api/combineReducers.html">
<i class="fa fa-check"></i>
<b>7.3.</b>
combineReducers
</a>
</li>
<li class="chapter " data-level="7.4" data-path="docs/api/applyMiddleware.html">
<a href="./docs/api/applyMiddleware.html">
<i class="fa fa-check"></i>
<b>7.4.</b>
applyMiddleware
</a>
</li>
<li class="chapter " data-level="7.5" data-path="docs/api/bindActionCreators.html">
<a href="./docs/api/bindActionCreators.html">
<i class="fa fa-check"></i>
<b>7.5.</b>
bindActionCreators
</a>
</li>
<li class="chapter " data-level="7.6" data-path="docs/api/compose.html">
<a href="./docs/api/compose.html">
<i class="fa fa-check"></i>
<b>7.6.</b>
compose
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="8" data-path="CHANGELOG.html">
<a href="./CHANGELOG.html">
<i class="fa fa-check"></i>
<b>8.</b>
Change Log
</a>
</li>
<li class="chapter " data-level="9" data-path="PATRONS.html">
<a href="./PATRONS.html">
<i class="fa fa-check"></i>
<b>9.</b>
Patrons
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
<div id="font-settings-wrapper" class="dropdown pull-left">
<a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" id="reduce-font-size" class="button size-2">A</button>
<button type="button" id="enlarge-font-size" class="button size-2">A</button>
</div>
<div class="buttons font-family-list">
<button type="button" data-font="0" class="button">Serif</button>
<button type="button" data-font="1" class="button">Sans</button>
</div>
<div class="buttons color-theme-list">
<button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
<button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
<button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
</div>
</div>
</div>
<!-- Actions Right -->
<div class="dropdown pull-right">
<a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
</a>
<div class="dropdown-menu font-settings dropdown-left">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" data-sharing="twitter" class="button">
Share on Twitter
</button>
<button type="button" data-sharing="google-plus" class="button">
Share on Google
</button>
<button type="button" data-sharing="facebook" class="button">
Share on Facebook
</button>
<button type="button" data-sharing="weibo" class="button">
Share on Weibo
</button>
<button type="button" data-sharing="instapaper" class="button">
Share on Instapaper
</button>
</div>
</div>
</div>
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Redux</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<a id="edit-link" href="https://github.com/rackt/redux/tree/master/README.md" class="btn fa fa-edit pull-left"> Edit This Page</a><h1 id="-redux-http-redux-js-org"><a href="http://redux.js.org" target="_blank">Redux</a></h1>
<p>Redux is a predictable state container for JavaScript apps. </p>
<p>It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as <a href="https://github.com/gaearon/redux-devtools" target="_blank">live code editing combined with a time traveling debugger</a>.</p>
<p>You can use Redux together with <a href="https://facebook.github.io/react/" target="_blank">React</a>, or with any other view library.<br>It is tiny (2kB, including dependencies).</p>
<p><a href="https://travis-ci.org/reactjs/redux" target="_blank"><img src="https://img.shields.io/travis/reactjs/redux/master.svg?style=flat-square" alt="build status"></a>
<a href="https://www.npmjs.com/package/redux" target="_blank"><img src="https://img.shields.io/npm/v/redux.svg?style=flat-square" alt="npm version"></a>
<a href="https://www.npmjs.com/package/redux" target="_blank"><img src="https://img.shields.io/npm/dm/redux.svg?style=flat-square" alt="npm downloads"></a>
<a href="https://discord.gg/0ZcbPKXt5bZ6au5t" target="_blank"><img src="https://img.shields.io/badge/discord-%23redux%20%40%20reactiflux-61dafb.svg?style=flat-square" alt="redux channel on discord"></a>
<a href="https://webchat.freenode.net/" target="_blank"><img src="https://img.shields.io/badge/irc-%23rackt%20%40%20freenode-61DAFB.svg?style=flat-square" alt="#rackt on freenode"></a>
<a href="https://changelog.com/187" target="_blank"><img src="https://img.shields.io/badge/changelog-%23187-lightgrey.svg?style=flat-square" alt="Changelog #187"></a></p>
<blockquote>
<p><strong>New! Learn Redux from its creator:<br><a href="https://egghead.io/series/getting-started-with-redux" target="_blank">Getting Started with Redux</a> (30 free videos)</strong></p>
</blockquote>
<h3 id="testimonials">Testimonials</h3>
<blockquote>
<p><a href="https://twitter.com/jingc/status/616608251463909376" target="_blank">“Love what you’re doing with Redux”</a><br>Jing Chen, creator of Flux</p>
<p><a href="https://twitter.com/fisherwebdev/status/616286955693682688" target="_blank">“I asked for comments on Redux in FB's internal JS discussion group, and it was universally praised. Really awesome work.”</a><br>Bill Fisher, author of Flux documentation</p>
<p><a href="https://twitter.com/andrestaltz/status/616271392930201604" target="_blank">“It's cool that you are inventing a better Flux by not doing Flux at all.”</a><br>André Staltz, creator of Cycle</p>
</blockquote>
<h3 id="developer-experience">Developer Experience</h3>
<p>I wrote Redux while working on my React Europe talk called <a href="https://www.youtube.com/watch?v=xsSnOQynTHs" target="_blank">“Hot Reloading with Time Travel”</a>. My goal was to create a state management library with minimal API but completely predictable behavior, so it is possible to implement logging, hot reloading, time travel, universal apps, record and replay, without any buy-in from the developer.</p>
<h3 id="influences">Influences</h3>
<p>Redux evolves the ideas of <a href="http://facebook.github.io/flux/" target="_blank">Flux</a>, but avoids its complexity by taking cues from <a href="https://github.com/evancz/elm-architecture-tutorial/" target="_blank">Elm</a>.<br>Whether you have used them or not, Redux only takes a few minutes to get started with.</p>
<h3 id="installation">Installation</h3>
<p>To install the stable version:</p>
<pre><code>npm install --save redux
</code></pre><p>Most likely, you’ll also need <a href="https://github.com/reactjs/react-redux" target="_blank">the React bindings</a> and <a href="https://github.com/gaearon/redux-devtools" target="_blank">the developer tools</a>.</p>
<pre><code>npm install --save react-redux
npm install --save-dev redux-devtools
</code></pre><p>This assumes that you’re using <a href="https://www.npmjs.com/" target="_blank">npm</a> package manager with a module bundler like <a href="http://webpack.github.io" target="_blank">Webpack</a> or <a href="http://browserify.org/" target="_blank">Browserify</a> to consume <a href="http://webpack.github.io/docs/commonjs.html" target="_blank">CommonJS modules</a>.</p>
<p>If you don’t yet use <a href="https://www.npmjs.com/" target="_blank">npm</a> or a modern module bundler, and would rather prefer a single-file <a href="https://github.com/umdjs/umd" target="_blank">UMD</a> build that makes <code>Redux</code> available as a global object, you can grab a pre-built version from <a href="https://cdnjs.com/libraries/redux" target="_blank">cdnjs</a>. We <em>don’t</em> recommend this approach for any serious application, as most of the libraries complementary to Redux are only available on <a href="https://www.npmjs.com/" target="_blank">npm</a>.</p>
<h3 id="the-gist">The Gist</h3>
<p>The whole state of your app is stored in an object tree inside a single <em>store</em>.<br>The only way to change the state tree is to emit an <em>action</em>, an object describing what happened.<br>To specify how the actions transform the state tree, you write pure <em>reducers</em>.</p>
<p>That’s it!</p>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> createStore <span class="token punctuation">}</span> from <span class="token string">'redux'</span>
<span class="token comment" spellcheck="true">/**
* This is a reducer, a pure function with (state, action) => state signature.
* It describes how an action transforms the state into the next state.
*
* The shape of the state is up to you: it can be a primitive, an array, an object,
* or even an Immutable.js data structure. The only important part is that you should
* not mutate the state object, but return a new object if the state changes.
*
* In this example, we use a `switch` statement and strings, but you can use a helper that
* follows a different convention (such as function maps) if it makes sense for your project.
*/</span>
<span class="token keyword">function</span> <span class="token function">counter</span><span class="token punctuation">(</span>state <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">switch</span> <span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">case</span> <span class="token string">'INCREMENT'</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> state <span class="token operator">+</span> <span class="token number">1</span>
<span class="token keyword">case</span> <span class="token string">'DECREMENT'</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> state <span class="token operator">-</span> <span class="token number">1</span>
<span class="token keyword">default</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> state
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">// Create a Redux store holding the state of your app.</span>
<span class="token comment" spellcheck="true">// Its API is { subscribe, dispatch, getState }.</span>
<span class="token keyword">let</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>counter<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// You can subscribe to the updates manually, or use bindings to your view layer.</span>
store<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// The only way to mutate the internal state is to dispatch an action.</span>
<span class="token comment" spellcheck="true">// The actions can be serialized, logged or stored and later replayed.</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'INCREMENT'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// 1</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'INCREMENT'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// 2</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">{</span> type<span class="token punctuation">:</span> <span class="token string">'DECREMENT'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// 1</span>
</code></pre>
<p>Instead of mutating the state directly, you specify the mutations you want to happen with plain objects called <em>actions</em>. Then you write a special function called a <em>reducer</em> to decide how every action transforms the entire application’s state.</p>
<p>If you’re coming from Flux, there is a single important difference you need to understand. Redux doesn’t have a Dispatcher or support many stores. Instead, there is just a single store with a single root reducing function. As your app grows, instead of adding stores, you split the root reducer into smaller reducers independently operating on the different parts of the state tree. This is exactly like there is just one root component in a React app, but it is composed out of many small components.</p>
<p>This architecture might seem like an overkill for a counter app, but the beauty of this pattern is how well it scales to large and complex apps. It also enables very powerful developer tools, because it is possible to trace every mutation to the action that caused it. You can record user sessions and reproduce them just by replaying every action.</p>
<h3 id="learn-redux-from-its-creator">Learn Redux from Its Creator</h3>
<p><a href="https://egghead.io/series/getting-started-with-redux" target="_blank">Getting Started with Redux</a> is a video course consisting of 30 videos narrated by Dan Abramov, author of Redux. It is designed to complement the “Basics” part of the docs while bringing additional insights about immutability, testing, Redux best practices, and using Redux with React. <strong>This course is free and will always be.</strong></p>
<blockquote>
<p><a href="https://twitter.com/sandrinodm/status/670548531422326785" target="_blank">“Great course on egghead.io by @dan_abramov - instead of just showing you how to use #redux, it also shows how and why redux was built!”</a><br>Sandrino Di Mattia</p>
<p><a href="https://twitter.com/chrisdhanaraj/status/670328025553219584" target="_blank">“Plowing through @dan_abramov 'Getting Started with Redux' - its amazing how much simpler concepts get with video.”</a><br>Chris Dhanaraj</p>
<p><a href="https://twitter.com/eddiezane/status/670333133242408960" target="_blank">“This video series on Redux by @dan_abramov on @eggheadio is spectacular!”</a><br>Eddie Zaneski</p>
<p><a href="https://twitter.com/danott/status/669909126554607617" target="_blank">“Come for the name hype. Stay for the rock solid fundamentals. (Thanks, and great job @dan_abramov and @eggheadio!)”</a><br>Dan</p>
<p><a href="https://twitter.com/gelatindesign/status/669658358643892224" target="_blank">“This series of videos on Redux by @dan_abramov is repeatedly blowing my mind - gunna do some serious refactoring”</a><br>Laurence Roberts</p>
</blockquote>
<p>So, what are you waiting for?</p>
<h4 id="-watch-the-30-free-videos-https-egghead-io-series-getting-started-with-redux"><a href="https://egghead.io/series/getting-started-with-redux" target="_blank">Watch the 30 Free Videos!</a></h4>
<p>If you enjoyed my course, consider supporting Egghead by <a href="https://egghead.io/pricing" target="_blank">buying a subscription</a>. Subscribers have access to the source code for the example in every one of my videos, as well as to tons of advanced lessons on other topics, including JavaScript in depth, React, Angular, and more. Many <a href="https://egghead.io/instructors" target="_blank">Egghead instructors</a> are also open source library authors, so buying a subscription is a nice way to thank them for the work that they’ve done.</p>
<h3 id="documentation">Documentation</h3>
<ul>
<li><a href="http://redux.js.org/docs/introduction/index.html" target="_blank">Introduction</a></li>
<li><a href="http://redux.js.org/docs/basics/index.html" target="_blank">Basics</a></li>
<li><a href="http://redux.js.org/docs/advanced/index.html" target="_blank">Advanced</a></li>
<li><a href="http://redux.js.org/docs/recipes/index.html" target="_blank">Recipes</a></li>
<li><a href="http://redux.js.org/docs/Troubleshooting.html" target="_blank">Troubleshooting</a></li>
<li><a href="http://redux.js.org/docs/Glossary.html" target="_blank">Glossary</a></li>
<li><a href="http://redux.js.org/docs/api/index.html" target="_blank">API Reference</a></li>
</ul>
<p>For PDF, ePub, and MOBI exports for offline reading, and instructions on how to create them, please see: <a href="https://github.com/paulkogel/redux-offline-docs" target="_blank">paulkogel/redux-offline-docs</a>.</p>
<h3 id="examples">Examples</h3>
<ul>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#counter-vanilla" target="_blank">Counter Vanilla</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/counter-vanilla" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#counter" target="_blank">Counter</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/counter" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#todos" target="_blank">Todos</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/todos" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#todos-with-undo" target="_blank">Todos with Undo</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/todos-with-undo" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#todomvc" target="_blank">TodoMVC</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/todomvc" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#shopping-cart" target="_blank">Shopping Cart</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/shopping-cart" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#tree-view" target="_blank">Tree View</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/tree-view" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#async" target="_blank">Async</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/async" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#universal" target="_blank">Universal</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/universal" target="_blank">source</a>)</li>
<li><a href="http://redux.js.org/docs/introduction/Examples.html#real-world" target="_blank">Real World</a> (<a href="https://github.com/reactjs/redux/tree/master/examples/real-world" target="_blank">source</a>)</li>
</ul>
<p>If you’re new to the NPM ecosystem and have troubles getting a project up and running, or aren’t sure where to paste the gist above, check out <a href="https://github.com/jackielii/simplest-redux-example" target="_blank">simplest-redux-example</a> that uses Redux together with React and Browserify.</p>
<h3 id="discussion">Discussion</h3>
<p>Join the <a href="https://discord.gg/0ZcbPKXt5bZ6au5t" target="_blank">#redux</a> channel of the <a href="http://www.reactiflux.com" target="_blank">Reactiflux</a> Discord community.</p>
<h3 id="thanks">Thanks</h3>
<ul>
<li><a href="https://github.com/evancz/elm-architecture-tutorial" target="_blank">The Elm Architecture</a> for a great intro to modeling state updates with reducers;</li>
<li><a href="http://www.confluent.io/blog/turning-the-database-inside-out-with-apache-samza/" target="_blank">Turning the database inside-out</a> for blowing my mind;</li>
<li><a href="https://www.youtube.com/watch?v=j-kj2qwJa_E" target="_blank">Developing ClojureScript with Figwheel</a> for convincing me that re-evaluation should “just work”;</li>
<li><a href="https://github.com/webpack/docs/wiki/hot-module-replacement-with-webpack" target="_blank">Webpack</a> for Hot Module Replacement;</li>
<li><a href="https://github.com/acdlite/flummox" target="_blank">Flummox</a> for teaching me to approach Flux without boilerplate or singletons;</li>
<li><a href="https://github.com/threepointone/disto" target="_blank">disto</a> for a proof of concept of hot reloadable Stores;</li>
<li><a href="https://github.com/optimizely/nuclear-js" target="_blank">NuclearJS</a> for proving this architecture can be performant;</li>
<li><a href="https://github.com/omcljs/om" target="_blank">Om</a> for popularizing the idea of a single state atom;</li>
<li><a href="https://github.com/cyclejs/cycle-core" target="_blank">Cycle</a> for showing how often a function is the best tool;</li>
<li><a href="https://github.com/facebook/react" target="_blank">React</a> for the pragmatic innovation.</li>
</ul>
<p>Special thanks to <a href="http://jdpaton.github.io" target="_blank">Jamie Paton</a> for handing over the <code>redux</code> NPM package name.</p>
<h3 id="change-log">Change Log</h3>
<p>This project adheres to <a href="http://semver.org/" target="_blank">Semantic Versioning</a>.<br>Every release, along with the migration instructions, is documented on the Github <a href="https://github.com/reactjs/redux/releases" target="_blank">Releases</a> page.</p>
<h3 id="patrons">Patrons</h3>
<p>The work on Redux was <a href="https://www.patreon.com/reactdx" target="_blank">funded by the community</a>.<br>Meet some of the outstanding companies that made it possible:</p>
<ul>
<li><a href="https://github.com/webflow" target="_blank">Webflow</a></li>
<li><a href="https://www.ximedes.com/" target="_blank">Ximedes</a></li>
</ul>
<p><a href="PATRONS.html">See the full list of Redux patrons.</a></p>
<h3 id="license">License</h3>
<p>MIT</p>
</section>
</div>
</div>
</div>
<a href="./docs/introduction/index.html" class="navigation navigation-next navigation-unique" aria-label="Next page: Introduction"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/plugins/gitbook-plugin-edit-link/plugin.js"></script>
<script src="gitbook/plugins/gitbook-plugin-github/plugin.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"edit-link":{"base":"https://github.com/rackt/redux/tree/master","label":"Edit This Page"},"github":{"url":"https://github.com/rackt/redux/"}};
gitbook.start(config);
});
</script>
</body>
</html>