Skip to content

Commit 0d9325e

Browse files
wgao19markerikson
authored andcommitted
Docs/refactor getting started (#1054)
* Refactor docs to contain a quick start and move getting started to "basic tutorial" * Follow up doc site updates
1 parent ba6b010 commit 0d9325e

File tree

7 files changed

+124
-105
lines changed

7 files changed

+124
-105
lines changed

docs/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Table of Contents
22

3-
- [Getting Started: adding React-Redux to a React todo app](./getting-started.md)
3+
- Introduction
4+
- [Quick Start: adding React-Redux to a React todo app](./introduction/quick-start.md)
5+
- [Basic Tutorial](./introduction/basic-tutorial.md)
46
- Using React-Redux
57
- [Connect: Extracting Data with `mapStateToProps`](./using-react-redux/connect-extracting-data-with-mapStateToProps.md)
68
- [API](api.md#api)

docs/getting-started.md renamed to docs/introduction/basic-tutorial.md

Lines changed: 40 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,21 @@
11
---
2-
id: getting-started
3-
title: Getting Started
2+
id: basic-tutorial
3+
title: Basic Tutorial
44
hide_title: true
5-
sidebar_label: Getting Started
5+
sidebar_label: Basic Tutorial
66
---
77

8-
# Getting Started
8+
# Basic Tutorial
99

10-
[React-Redux](https://github.com/reduxjs/react-redux) is the official [React](https://reactjs.org/) binding for [Redux](https://redux.js.org/). It lets your React components read data from a Redux store, and dispatch actions to the store to update data.
11-
12-
## Installation
13-
14-
To use React-Redux with your React app:
15-
16-
```bash
17-
npm install --save react-redux
18-
```
19-
20-
or
21-
22-
```bash
23-
yarn add react-redux
24-
```
25-
26-
<!-- perhaps add link to an extra quick start section? -->
27-
28-
## `Provider` and `connect`
29-
30-
React-Redux consists of two main pieces. The first is a component called `<Provider />`, which makes the Redux store available to the rest of your app:
31-
32-
```js
33-
import React from "react";
34-
import ReactDOM from "react-dom";
35-
36-
import { Provider } from "react-redux";
37-
import store from "./store";
38-
39-
import App from "./App";
40-
41-
const rootElement = document.getElementById("root");
42-
ReactDOM.render(
43-
<Provider store={store}>
44-
<App />
45-
</Provider>,
46-
rootElement
47-
);
48-
```
49-
50-
The second piece is a function called `connect()`, which encapsulates the process of talking to the store.
51-
52-
It enables you to:
53-
54-
- Read data from the Redux `store` into your app’s connected components as props
55-
- Dispatch actions to your `store` from any of your app’s connected components
56-
57-
Correspondingly, the `connect` function takes two arguments, both optional:
58-
59-
- `mapStateToProps`: called every time the store state changes. It receives the entire store state, and should return an object of data this component needs.
60-
61-
- `mapDispatchToProps`: this parameter can either be a function, or an object.
62-
- If it’s a function, it will be called once on component creation. It will receive `dispatch` as an argument, and should return an object full of functions that use `dispatch` to dispatch actions.
63-
- If it’s an object full of action creators, each action creator will be turned into a prop function that automatically dispatches its action when called. **Note**: We recommend using this “object shorthand” form.
64-
65-
Normally, you’ll call `connect` in this way:
66-
67-
```js
68-
const mapStateToProps = (state, ownProps) => ({
69-
// ... computed data from state and optionally ownProps
70-
});
71-
72-
const mapDispatchToProps = {
73-
// ... normally is an object full of action creators
74-
};
75-
76-
// `connect` returns a new function that accepts the component to wrap:
77-
const connectToStore = connect(
78-
mapStateToProps,
79-
mapDispatchToProps
80-
);
81-
// and that function returns the connected, wrapper component:
82-
const ConnectedComponent = connectToStore(Component);
83-
84-
// We normally do both in one step, like this:
85-
connect(
86-
mapStateToProps,
87-
mapDispatchToProps
88-
)(Component);
89-
```
10+
To see how to use React-Redux in practice, we’ll show a step-by-step example by creating a todo list app.
9011

9112
## A Todo List Example
9213

93-
To see this in practice, we’ll show a step-by-step example by creating a todo list app using React-Redux.
94-
9514
**Jump to**
9615

9716
- 🤞 [Just show me the code](https://codesandbox.io/s/9on71rvnyo)
9817
- 👆 [Providing the store](#providing-the-store)
99-
- ✌️ [Common Ways of Calling Connect](#common-ways-of-calling-connect)
18+
- ✌️ [Connecting the Component](#connecting-the-components)
10019

10120
**The React UI Components**
10221

@@ -177,9 +96,41 @@ Notice how our `<TodoApp />` is now wrapped with the `<Provider />` with `store`
17796

17897
### Connecting the Components
17998

180-
Our components need to read values from the Redux store (and re-read the values when the store updates). They also need to dispatch actions to trigger updates.
99+
React-Redux provides a `connect` function for you to read values from the Redux store (and re-read the values when the store updates).
100+
101+
The `connect` function takes two arguments, both optional:
102+
103+
- `mapStateToProps`: called every time the store state changes. It receives the entire store state, and should return an object of data this component needs.
104+
105+
- `mapDispatchToProps`: this parameter can either be a function, or an object.
106+
- If it’s a function, it will be called once on component creation. It will receive `dispatch` as an argument, and should return an object full of functions that use `dispatch` to dispatch actions.
107+
- If it’s an object full of action creators, each action creator will be turned into a prop function that automatically dispatches its action when called. **Note**: We recommend using this “object shorthand” form.
108+
109+
Normally, you’ll call `connect` in this way:
181110

182-
`connect` takes in two parameters. The first one allows you to define which pieces of data from the store are needed by this component. The second one allows you to indicate which actions that component might dispatch. By convention, they are called `mapStateToProps` and `mapDispatchToProps`, respectively. The return of this call is another function that accepts the component on a second call. This is an example of a pattern called [_higher order components_](https://medium.com/@franleplant/react-higher-order-components-in-depth-cf9032ee6c3e).
111+
```js
112+
const mapStateToProps = (state, ownProps) => ({
113+
// ... computed data from state and optionally ownProps
114+
});
115+
116+
const mapDispatchToProps = {
117+
// ... normally is an object full of action creators
118+
};
119+
120+
// `connect` returns a new function that accepts the component to wrap:
121+
const connectToStore = connect(
122+
mapStateToProps,
123+
mapDispatchToProps
124+
);
125+
// and that function returns the connected, wrapper component:
126+
const ConnectedComponent = connectToStore(Component);
127+
128+
// We normally do both in one step, like this:
129+
connect(
130+
mapStateToProps,
131+
mapDispatchToProps
132+
)(Component);
133+
```
183134

184135
Let’s work on `<AddTodo />` first. It needs to trigger changes to the `store` to add new todos. Therefore, it needs to be able to `dispatch` actions to the store. Here’s how we do it.
185136

docs/introduction/quick-start.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
id: quick-start
3+
title: Quick Start
4+
hide_title: true
5+
sidebar_label: Quick Start
6+
---
7+
8+
# Quick Start
9+
10+
[React-Redux](https://github.com/reduxjs/react-redux) is the official [React](https://reactjs.org/) binding for [Redux](https://redux.js.org/). It lets your React components read data from a Redux store, and dispatch actions to the store to update data.
11+
12+
## Installation
13+
14+
To use React-Redux with your React app:
15+
16+
```bash
17+
npm install --save react-redux
18+
```
19+
20+
or
21+
22+
```bash
23+
yarn add react-redux
24+
```
25+
26+
## `Provider` and `connect`
27+
28+
React-Redux provides `<Provider />`, which makes the Redux store available to the rest of your app:
29+
30+
```js
31+
import React from "react";
32+
import ReactDOM from "react-dom";
33+
34+
import { Provider } from "react-redux";
35+
import store from "./store";
36+
37+
import App from "./App";
38+
39+
const rootElement = document.getElementById("root");
40+
ReactDOM.render(
41+
<Provider store={store}>
42+
<App />
43+
</Provider>,
44+
rootElement
45+
);
46+
```
47+
48+
React-Redux provides a `connect` function for you to connect your component to the store.
49+
50+
Normally, you’ll call `connect` in this way:
51+
52+
```js
53+
import { connect } from "react-redux";
54+
import { increment, decrement, reset } from "./actionCreators";
55+
56+
// const Counter = ...
57+
58+
const mapStateToProps = (state /*, ownProps*/) => {
59+
return {
60+
counter: state.counter
61+
};
62+
};
63+
64+
const mapDispatchToProps = { increment, decrement, reset };
65+
66+
export default connect(
67+
mapStateToProps,
68+
mapDispatchToProps
69+
)(Counter);
70+
```

website/core/Footer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Footer extends React.Component {
3434
</a>
3535
<div>
3636
<h5>Docs</h5>
37-
<a href={this.docUrl("getting-started")}>Introduction</a>
37+
<a href={this.docUrl("introduction/quick-start")}>Introduction</a>
3838
<a
3939
href={this.docUrl(
4040
"using-react-redux/connect-extracting-data-with-mapStateToProps"

website/pages/en/index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ const ProjectTitle = () => (
5151
<h2 className="projectTitle">
5252
{siteConfig.title}
5353
<small>
54-
<MarkdownBlock>
55-
Official React bindings for [Redux](https://github.com/reduxjs/redux)
56-
</MarkdownBlock></small>
54+
<MarkdownBlock>
55+
Official React bindings for [Redux](https://github.com/reduxjs/redux)
56+
</MarkdownBlock>
57+
</small>
5758
</h2>
5859
);
5960

@@ -73,8 +74,8 @@ class HomeSplash extends React.Component {
7374
<div className="inner">
7475
<ProjectTitle />
7576
<PromoSection>
76-
<Button href={docUrl("getting-started", language)}>
77-
Get started
77+
<Button href={docUrl("introduction/quick-start", language)}>
78+
Quick Start
7879
</Button>
7980
<Button href="https://github.com/reduxjs/react-redux">
8081
Github
@@ -92,13 +93,8 @@ const Installation = () => (
9293
style={{ textAlign: "center" }}
9394
>
9495
<h2>Installation</h2>
95-
<MarkdownBlock>
96-
React Redux requires **React 0.14 or later.**
97-
</MarkdownBlock>
98-
<MarkdownBlock>
99-
``` npm install --save
100-
react-redux ```
101-
</MarkdownBlock>
96+
<MarkdownBlock>React Redux requires **React 0.14 or later.**</MarkdownBlock>
97+
<MarkdownBlock>``` npm install --save react-redux ```</MarkdownBlock>
10298
</div>
10399
);
104100

website/sidebars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"docs": {
3-
"Introduction": ["getting-started"],
3+
"Introduction": ["introduction/quick-start", "introduction/basic-tutorial"],
44
"Using React-Redux": [
55
"using-react-redux/connect-extracting-data-with-mapStateToProps"
66
],

website/siteConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const siteConfig = {
2626

2727
// For no header links in the top nav bar -> headerLinks: [],
2828
headerLinks: [
29-
{ doc: "getting-started", label: "Getting Started" },
29+
{ doc: "introduction/quick-start", label: "Quick Start" },
3030
{
3131
doc: "using-react-redux/connect-extracting-data-with-mapStateToProps",
3232
label: "Using React-Redux"

0 commit comments

Comments
 (0)