Skip to content

Commit ad7c9ca

Browse files
committed
Revert README change and set embeds on examples page
1 parent 96db635 commit ad7c9ca

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

README.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,53 @@ To specify how the actions transform the state tree, you write pure *reducers*.
128128

129129
That's it!
130130

131-
<iframe src="https://codesandbox.io/embed/zqq52wqj5p?hidenavigation=1&view=split&previewwindow=console&verticallayout=1&fontsize=12&runonclick=0&codemirror=1&editorsize=75" style="width:100%; height:800px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>
132-
131+
```js
132+
import { createStore } from 'redux'
133+
134+
/**
135+
* 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+
function counter(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+
```
133178

134179
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.
135180

docs/introduction/Examples.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ npm start
2929

3030
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/counter):
3131

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}}" %}
32+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/counter" %}
3333

3434
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.
3535

@@ -49,6 +49,8 @@ npm start
4949

5050
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos).
5151

52+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos" %}
53+
5254
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.
5355

5456
This example includes tests.
@@ -67,6 +69,8 @@ npm start
6769

6870
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos-with-undo).
6971

72+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todos-with-undo" %}
73+
7074
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.
7175

7276
## TodoMVC
@@ -83,6 +87,8 @@ npm start
8387

8488
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todomvc).
8589

90+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/todomvc" %}
91+
8692
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.
8793

8894
This example includes tests.
@@ -101,6 +107,8 @@ npm start
101107

102108
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/shopping-cart).
103109

110+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/shopping-cart" %}
111+
104112
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.
105113

106114
## Tree View
@@ -117,6 +125,8 @@ npm start
117125

118126
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/tree-view).
119127

128+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/tree-view" %}
129+
120130
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.
121131

122132
This example includes tests.
@@ -135,6 +145,8 @@ npm start
135145

136146
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/async).
137147

148+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/async" %}
149+
138150
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.
139151

140152
## Universal
@@ -165,6 +177,8 @@ npm start
165177

166178
Or check out the [sandbox](https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/real-world).
167179

180+
{% embed url="https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/real-world" %}
181+
168182
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.
169183

170184
## More Examples

0 commit comments

Comments
 (0)