You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* This is a reducer, a pure function with (state, action) => state signature.
136
+
* It describes how an action transforms the state into the next state.
137
+
*
138
+
* The shape of the state is up to you: it can be a primitive, an array, an object,
139
+
* or even an Immutable.js data structure. The only important part is that you should
140
+
* not mutate the state object, but return a new object if the state changes.
141
+
*
142
+
* In this example, we use a `switch` statement and strings, but you can use a helper that
143
+
* follows a different convention (such as function maps) if it makes sense for your
144
+
* project.
145
+
*/
146
+
functioncounter(state=0, action) {
147
+
switch (action.type) {
148
+
case'INCREMENT':
149
+
return state +1
150
+
case'DECREMENT':
151
+
return state -1
152
+
default:
153
+
return state
154
+
}
155
+
}
156
+
157
+
// Create a Redux store holding the state of your app.
158
+
// Its API is { subscribe, dispatch, getState }.
159
+
let store =createStore(counter)
160
+
161
+
// You can use subscribe() to update the UI in response to state changes.
162
+
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
163
+
// However it can also be handy to persist the current state in the localStorage.
164
+
165
+
store.subscribe(() =>
166
+
console.log(store.getState())
167
+
)
168
+
169
+
// The only way to mutate the internal state is to dispatch an action.
170
+
// The actions can be serialized, logged or stored and later replayed.
171
+
store.dispatch({ type:'INCREMENT' })
172
+
// 1
173
+
store.dispatch({ type:'INCREMENT' })
174
+
// 2
175
+
store.dispatch({ type:'DECREMENT' })
176
+
// 1
177
+
```
133
178
134
179
Instead of mutating the state directly, you specify the mutations you want to happen with plain objects called *actions*. Then you write a special function called a *reducer* to decide how every action transforms the entire application's state.
Copy file name to clipboardExpand all lines: docs/introduction/Examples.md
+15-1
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ npm start
29
29
30
30
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/counter):
31
31
32
-
{% embed data="{\"url\":\"https://codesandbox.io/embed/github/reactjs/redux/tree/master/examples/counter\",\"type\":\"link\",\"title\":\"CodeSandbox\",\"description\":\"CodeSandbox is an online editor tailored for web applications.\",\"icon\":{\"type\":\"icon\",\"url\":\"https://codesandbox.io/favicon.ico\",\"aspectRatio\":0},\"thumbnail\":{\"type\":\"thumbnail\",\"url\":\"https://codesandbox.io/static/img/banner.png\",\"width\":1200,\"height\":630,\"aspectRatio\":0.525}}" %}
This is the most basic example of using Redux together with React. For simplicity, it re-renders the React component manually when the store changes. In real projects, you will likely want to use the highly performant [React Redux](https://github.com/reactjs/react-redux) bindings instead.
35
35
@@ -49,6 +49,8 @@ npm start
49
49
50
50
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos).
This is the best example to get a deeper understanding of how the state updates work together with components in Redux. It shows how reducers can delegate handling actions to other reducers, and how you can use [React Redux](https://github.com/reactjs/react-redux) to generate container components from your presentational components.
53
55
54
56
This example includes tests.
@@ -67,6 +69,8 @@ npm start
67
69
68
70
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos-with-undo).
This is a variation on the previous example. It is almost identical, but additionally shows how wrapping your reducer with [Redux Undo](https://github.com/omnidan/redux-undo) lets you add a Undo/Redo functionality to your app with a few lines of code.
71
75
72
76
## TodoMVC
@@ -83,6 +87,8 @@ npm start
83
87
84
88
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todomvc).
This is the classical [TodoMVC](http://todomvc.com/) example. It's here for the sake of comparison, but it covers the same points as the Todos example.
87
93
88
94
This example includes tests.
@@ -101,6 +107,8 @@ npm start
101
107
102
108
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/shopping-cart).
This example shows important idiomatic Redux patterns that become important as your app grows. In particular, it shows how to store entities in a normalized way by their IDs, how to compose reducers on several levels, and how to define selectors alongside the reducers so the knowledge about the state shape is encapsulated. It also demonstrates logging with [Redux Logger](https://github.com/fcomb/redux-logger) and conditional dispatching of actions with [Redux Thunk](https://github.com/gaearon/redux-thunk) middleware.
105
113
106
114
## Tree View
@@ -117,6 +125,8 @@ npm start
117
125
118
126
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/tree-view).
This example demonstrates rendering a deeply nested tree view and representing its state in a normalized form so it is easy to update from reducers. Good rendering performance is achieved by the container components granularly subscribing only to the tree nodes that they render.
121
131
122
132
This example includes tests.
@@ -135,6 +145,8 @@ npm start
135
145
136
146
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/async).
This example includes reading from an asynchronous API, fetching data in response to user input, showing loading indicators, caching the response, and invalidating the cache. It uses [Redux Thunk](https://github.com/gaearon/redux-thunk) middleware to encapsulate asynchronous side effects.
139
151
140
152
## Universal
@@ -165,6 +177,8 @@ npm start
165
177
166
178
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/real-world).
This is the most advanced example. It is dense by design. It covers keeping fetched entities in a normalized cache, implementing a custom middleware for API calls, rendering partially loaded data, pagination, caching responses, displaying error messages, and routing. Additionally, it includes Redux DevTools.
0 commit comments