Skip to content

Commit f9bb84c

Browse files
giuseppegphilipp-spiess
authored andcommitted
Update README.md (#1)
* Update README.md Some suggestions. In *Getting Started* I would also mention `RePureComponent` and explain how to also pass a `payload` with actions right away. People usually look for `TL;DR` :D * Update README.md * Format README
1 parent 8467418 commit f9bb84c

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626

2727
---
2828

29-
There are numerous solutions to managing state in React including powerful libraries like [Redux][] and architectures like [Flux] which evolves around the concept of a "reducer": A single handler for all actions in one store. Recent development of [Reason][] builds upon this concepts and integrates it tightly into [ReasonReact][] by the use of a "reducer component".
29+
A number of solutions to manage state in React applications are based on the concept of "reducer" – a function that transforms the state of the application in response to actions. An example is the [Redux][] library and architectures like [Flux][].
3030

31-
A reducer component is used like a regular, stateful, React component with the added twist that `setState` is not allowed. Instead, state is updated through a `reducer` which is triggered by sending actions to it. ReComponent brings this concept to your React application today.
31+
Most recently this pattern was implemented in [ReasonReact][] as the built-in solution to manage local component state. Similarly to Redux, ReasonReact components implement a reducer and actions to trigger state changes. Stateful components are usually referred as "reducer component".
32+
33+
_ReComponent_ borrows these ideas from ReasonReact and brings reducer components to the React ecosystem.
34+
35+
A reducer component is used like a regular, stateful, React component with the added twist that `setState` is not allowed. Instead, state is updated through a `reducer` which is triggered by sending actions to it.
3236

3337
- [Installation](#installation)
3438
- [Getting Started](#getting-started)
@@ -47,7 +51,9 @@ npm install react-recomponent --save
4751

4852
## Getting Started
4953

50-
Using `ReComponent` works in the same way as using a regular React component. State is usually initialized using the `initialState()` callback and can only be modified by sending actions to the `reducer()` function. To help with that, you can use `createSender()`. Take a look at a simple counter example:
54+
To create a reducer component extend `ReComponent` from `react-recomponent` instead of `React.Component`.
55+
56+
With `ReComponent` state is usually initialized using the `initialState()` callback and can only be modified by sending actions to the `reducer()` function. To help with that, you can use `createSender()`. Take a look at a simple counter example:
5157

5258
```js
5359
import React from "react";
@@ -84,28 +90,28 @@ class Counter extends ReComponent {
8490

8591
[![Edit ReComponent - Getting Started](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/zq0210299x)
8692

87-
The `Counter` component starts with an initial state of `{ count: 0 }`. Note that this state is in fact a regular React component state. To update it, we use a click action which we identify by its type `"CLICK"` (This is similar to the way we actions are identified in Redux).
93+
The `Counter` component starts with an initial state of `{ count: 0 }`. Note that this state is in fact a regular React component state. To update it, we use a click action which we identify by its type `"CLICK"` (this is similar to the way actions are identified in Redux).
8894

8995
The `reducer` will receive this action and act accordingly. In our case, it will return an `Update()` effect with the modified state.
9096

9197
ReComponent comes with four different types of [effects](https://github.com/philipp-spiess/react-recomponent#effects):
9298

93-
- `NoUpdate()` to signalize that nothing should happen.
94-
- `Update(state)` to update the state.
95-
- `SideEffects(fn)` to run an arbitrary function to issue a [side effect]. Side effects may never be run directly inside the reducer. A reducer should always be pure: For the same action applied onto the same state, it should return the same effects. This paves the way to future react features like async React and make it easier to maintain your code.
96-
- `UpdateWithSideEffects(state, fn)` to do both: update the state and then trigger the side effect.
99+
- `NoUpdate()` signalize that nothing should happen.
100+
- `Update(state)` update the state.
101+
- `SideEffects(fn)` run an arbitrary function which has [side effect]s. Side effects may never be run directly inside the reducer. **A reducer should always be pure**: for the same action applied onto the same state, it should return the same effects. **This is to avoid bugs when React will work asynchronously**.
102+
- `UpdateWithSideEffects(state, fn)` both update the state and then trigger the side effect.
97103

98-
By intelligently returning any of the four types, it is possible to transition between states at one place and without the need to use `setState()` manually which drastically simplifies our mental model: Whatever happens, it must go through the reducer first.
104+
By intelligently using any of the four types above, it is possible to transition between states in one place and without the need to use `setState()` manually. This drastically simplifies our mental model since changes must always go through the reducer first.
99105

100106
## Advanced Usage
101107

102108
Now that we‘ve learned how to use reducer components with React, it‘s time to look into more advanced use cases to effectively handle state transitions across bigger portions of your app.
103109

104110
### Side Effects
105111

106-
We‘ve already heard that ReComponent comes with four different types of [effects](https://github.com/philipp-spiess/react-recomponent#effects). This is necessary to affectively handling side effects by keeping your reducer pure (given the same state and action, it will always return the same effects).
112+
We‘ve already said that ReComponent comes with four different types of [effects](https://github.com/philipp-spiess/react-recomponent#effects). This is necessary to effectively handle side effects by keeping your reducer pure given the same state and action, it will always return the same effects.
107113

108-
The following example will demonstrate the four different types of effects and shows you how to use them:
114+
The following example will demonstrate the four different types of effects and show you how to use them:
109115

110116
```js
111117
import React from "react";
@@ -170,9 +176,9 @@ class Counter extends ReComponent {
170176

171177
### Handling Events
172178

173-
React uses a method called pooling to improve performance when emitting events (check out the guides on [`SyntheticEvent`](https://reactjs.org/docs/events.html) to learn more). This means that events that React will trigger will be reused after the callback was handled.
179+
React uses a method called pooling to improve performance when emitting events (check out the guides on [`SyntheticEvent`](https://reactjs.org/docs/events.html) to learn more). Basically React recycles events once the callback is handled making any reference to them unavailable.
174180

175-
Since the reducer function always runs within the `setState()` callback provided by React, synthetic events will already be recycled. To still be able to access event properties, we recommend passing the required values explicitly. The following example will show the coordinates of the last mouse click. To have control over which properties are sent to the reducer, we‘re using `send` directly in this case:
181+
Since the reducer function always runs within the `setState()` callback provided by React, synthetic events will already be recycled by the time the reducer is invoked. To be able to access event properties, we recommend passing the required values explicitly. The following example will show the coordinates of the last mouse click. To have control over which properties are sent to the reducer, we‘re using `send` directly in this case:
176182

177183
```js
178184
import React from "react";
@@ -229,9 +235,9 @@ class Counter extends ReComponent {
229235

230236
### Manage State Across the Tree
231237

232-
You often want to pass state properties down to other children in order for them to behave properly. Some times, however, this tree is very deep and it might be inefficient to go through the whole tree (and thereby updating it) to pass a value down.
238+
Often times we want to pass state properties to descendants that are very deep in the application tree. In order to do so, the components in between need to pass those properties to their respective children until we reach the desired component. This pattern is usually called [prop drilling](https://blog.kentcdodds.com/prop-drilling-bb62e02cb691) and it is usually what you want to do.
233239

234-
Fortunately, React 16.3.0 introduced a new API called [`createContext()`](https://reactjs.org/docs/context.html#reactcreatecontext) which we can leverage to implement this.
240+
Sometimes, however, the layers in-between are expensive to re-render causing your application to become janky. Fortunately, React 16.3.0 introduced a new API called [`createContext()`](https://reactjs.org/docs/context.html#reactcreatecontext) that we can use to solve this issue by using context to pass those properties directly to the target component and skipping the update of all intermediate layers:
235241

236242
```js
237243
import React from "react";
@@ -430,7 +436,6 @@ Check out the [type definition tests](https://github.com/philipp-spiess/react-re
430436
[MIT](https://github.com/philipp-spiess/react-recomponent/blob/master/README.md)
431437

432438
[redux]: https://github.com/reduxjs/redux
433-
[reason]: https://reasonml.github.io/
434439
[reasonreact]: https://reasonml.github.io/reason-react/docs/en/state-actions-reducer.html
435440
[flux]: https://facebook.github.io/flux/
436441
[side effect]: https://en.wikipedia.org/wiki/Side_effect_(computer_science)

0 commit comments

Comments
 (0)