Skip to content

Commit e9094e7

Browse files
authored
Update docusaurus (#1638)
1 parent 90795b6 commit e9094e7

File tree

81 files changed

+18220
-7873
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+18220
-7873
lines changed

docs/using-react-redux/connect-dispatching-actions-with-mapDispatchToProps.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: connect-mapdispatch
3-
title: Connect: Dispatching Actions with mapDispatchToProps
3+
title: "Connect: Dispatching Actions with mapDispatchToProps"
44
hide_title: true
5-
sidebar_label: Connect: Dispatching Actions with mapDispatchToProps
5+
sidebar_label: "Connect: Dispatching Actions with mapDispatchToProps"
66
---
77

88
# Connect: Dispatching Actions with `mapDispatchToProps`

docs/using-react-redux/connect-extracting-data-with-mapStateToProps.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: connect-mapstate
3-
title: Connect: Extracting Data with mapStateToProps
3+
title: "Connect: Extracting Data with mapStateToProps"
44
hide_title: true
5-
sidebar_label: Connect: Extracting Data with mapStateToProps
5+
sidebar_label: "Connect: Extracting Data with mapStateToProps"
66
---
77

88
# Connect: Extracting Data with `mapStateToProps`
@@ -149,7 +149,7 @@ There are a few ways to approach this:
149149
150150
- Some transformations could be calculated in an action creator or reducer, and the transformed data could be kept in the store
151151
- Transformations can also be done in a component's `render()` method
152-
- If the transformation does need to be done in a `mapStateToProps` function, then we recommend using [memoized selector functions]() to ensure the transformation is only run when the input values have changed.
152+
- If the transformation does need to be done in a `mapStateToProps` function, then we recommend using [memoized selector functions](https://redux.js.org/recipes/computing-derived-data#creating-a-memoized-selector) to ensure the transformation is only run when the input values have changed.
153153
154154
#### Immutable.js Performance Concerns
155155
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
---
2+
id: static-typing
3+
title: Static Typing
4+
hide_title: true
5+
sidebar_label: Static Typing
6+
---
7+
8+
# Static Typing
9+
10+
React-Redux is currently written in plain JavaScript. However, it works well with static type systems such as TypeScript and Flow.
11+
12+
## TypeScript
13+
14+
React-Redux doesn't ship with its own type definitions. If you are using Typescript you should install the [`@types/react-redux` type definitions](https://npm.im/@types/react-redux) from npm. In addition to typing the library functions, the types also export some helpers to make it easier to write typesafe interfaces between your Redux store and your React components.
15+
16+
### Defining the Root State Type
17+
18+
Both `mapState` and `useSelector` depend on declaring the type of the complete Redux store state value. While this type could be written by hand, the easiest way to define it is to have TypeScript infer it based on what your root reducer function returns. This way, the type is automatically updated as the reducer functions are modified.
19+
20+
```ts
21+
// rootReducer.ts
22+
export const rootReducer = combineReducers({
23+
posts: postsReducer,
24+
comments: commentsReducer,
25+
users: usersReducer
26+
})
27+
28+
export type RootState = ReturnType<typeof rootReducer>
29+
// {posts: PostsState, comments: CommentsState, users: UsersState}
30+
```
31+
32+
### Typing the `useSelector` hook
33+
34+
When writing selector functions for use with `useSelector`, you should explicitly define the type of the `state` parameter. TS should be able to then infer the return type of the selector, which will be reused as the return type of the `useSelector` hook:
35+
36+
```ts
37+
interface RootState {
38+
isOn: boolean
39+
}
40+
41+
// TS infers type: (state: RootState) => boolean
42+
const selectIsOn = (state: RootState) => state.isOn
43+
44+
// TS infers `isOn` is boolean
45+
const isOn = useSelector(selectIsOn)
46+
```
47+
48+
If you want to avoid repeating the `state` type declaration, you can define a typed `useSelector` hook using a helper type exported by `@types/react-redux`:
49+
50+
```ts
51+
// reducer.ts
52+
import { useSelector, TypedUseSelectorHook } from 'react-redux'
53+
54+
interface RootState {
55+
isOn: boolean
56+
}
57+
58+
export const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector
59+
60+
// my-component.tsx
61+
import { useTypedSelector } from './reducer.ts'
62+
63+
const isOn = useTypedSelector(state => state.isOn)
64+
```
65+
66+
### Typing the `useDispatch` hook
67+
68+
By default, the return value of `useDispatch` is the standard `Dispatch` type defined by the Redux core types, so no declarations are needed:
69+
70+
```ts
71+
const dispatch = useDispatch()
72+
```
73+
74+
If you have a customized version of the `Dispatch` type, you may use that type explicitly:
75+
76+
```ts
77+
// store.ts
78+
export type AppDispatch = typeof store.dispatch
79+
80+
// MyComponent.tsx
81+
const dispatch: AppDispatch = useDispatch()
82+
```
83+
84+
### Typing the `connect` higher order component
85+
86+
#### Manually Typing `connect`
87+
88+
The `connect` higher-order component is somewhat complex to type, because there are 3 sources of props: `mapStateToProps`, `mapDispatchToProps`, and props passed in from the parent component. Here's a full example of what it looks like to do that manually.
89+
90+
```tsx
91+
import { connect } from 'react-redux'
92+
93+
interface StateProps {
94+
isOn: boolean
95+
}
96+
97+
interface DispatchProps {
98+
toggleOn: () => void
99+
}
100+
101+
interface OwnProps {
102+
backgroundColor: string
103+
}
104+
105+
type Props = StateProps & DispatchProps & OwnProps
106+
107+
const mapState = (state: RootState) => ({
108+
isOn: state.isOn
109+
})
110+
111+
const mapDispatch = {
112+
toggleOn: () => ({ type: 'TOGGLE_IS_ON' })
113+
}
114+
115+
const MyComponent = (props: Props) => (
116+
<div style={{ backgroundColor: props.backgroundColor }}>
117+
<button onClick={props.toggleOn}>
118+
Toggle is {props.isOn ? 'ON' : 'OFF'}
119+
</button>
120+
</div>
121+
)
122+
123+
// Typical usage: `connect` is called after the component is defined
124+
export default connect<StateProps, DispatchProps, OwnProps>(
125+
mapState,
126+
mapDispatch
127+
)(MyComponent)
128+
```
129+
130+
It is also possible to shorten this somewhat, by inferring the types of `mapState` and `mapDispatch`:
131+
132+
```ts
133+
const mapState = (state: RootState) => ({
134+
isOn: state.isOn
135+
})
136+
137+
const mapDispatch = {
138+
toggleOn: () => ({ type: 'TOGGLE_IS_ON' })
139+
}
140+
141+
type StateProps = ReturnType<typeof mapState>
142+
type DispatchProps = typeof mapDispatch
143+
144+
type Props = StateProps & DispatchProps & OwnProps
145+
```
146+
147+
However, inferring the type of `mapDispatch` this way will break if it is defined as an object and also refers to thunks.
148+
149+
#### Inferring The Connected Props Automatically
150+
151+
`connect` consists of two functions that are called sequentially. The first function accepts `mapState` and `mapDispatch` as arguments, and returns a second function. The second function accepts the component to be wrapped, and returns a new wrapper component that passes down the props from `mapState` and `mapDispatch`. Normally, both functions are called together, like `connect(mapState, mapDispatch)(MyComponent)`.
152+
153+
As of v7.1.2, the `@types/react-redux` package exposes a helper type, `ConnectedProps`, that can extract the return types of `mapStateToProps` and `mapDispatchToProps` from the first function. This means that if you split the `connect` call into two steps, all of the "props from Redux" can be inferred automatically without having to write them by hand. While this approach may feel unusual if you've been using React-Redux for a while, it does simplify the type declarations considerably.
154+
155+
```ts
156+
import { connect, ConnectedProps } from 'react-redux'
157+
158+
interface RootState {
159+
isOn: boolean
160+
}
161+
162+
const mapState = (state: RootState) => ({
163+
isOn: state.isOn
164+
})
165+
166+
const mapDispatch = {
167+
toggleOn: () => ({ type: 'TOGGLE_IS_ON' })
168+
}
169+
170+
const connector = connect(
171+
mapState,
172+
mapDispatch
173+
)
174+
175+
// The inferred type will look like:
176+
// {isOn: boolean, toggleOn: () => void}
177+
type PropsFromRedux = ConnectedProps<typeof connector>
178+
```
179+
180+
The return type of `ConnectedProps` can then be used to type your props object.
181+
182+
```tsx
183+
interface Props extends PropsFromRedux {
184+
backgroundColor: string
185+
}
186+
187+
const MyComponent = (props: Props) => (
188+
<div style={{ backgroundColor: props.backgroundColor }}>
189+
<button onClick={props.toggleOn}>
190+
Toggle is {props.isOn ? 'ON' : 'OFF'}
191+
</button>
192+
</div>
193+
)
194+
195+
export default connector(MyComponent)
196+
```
197+
198+
Because types can be defined in any order, you can still declare your component before declaring the connector if you want.
199+
200+
```tsx
201+
// alternately, declare `type Props = Props From Redux & {backgroundColor: string}`
202+
interface Props extends PropsFromRedux {
203+
backgroundColor: string;
204+
}
205+
206+
const MyComponent = (props: Props) => /* same as above */
207+
208+
const connector = connect(/* same as above*/)
209+
210+
type PropsFromRedux = ConnectedProps<typeof connector>
211+
212+
export default connector(MyComponent)
213+
```
214+
215+
### Recommendations
216+
217+
The hooks API is generally simpler to use with static types. **If you're looking for the easiest solution for using static types with React-Redux, use the hooks API.**
218+
219+
If you're using `connect`, **we recommend using the `ConnectedProps<T>` approach for inferring the props from Redux**, as that requires the fewest explicit type declarations.
220+
221+
## Resources
222+
223+
For additional information, see these additional resources:
224+
225+
- [Redux docs: Usage with TypeScript](https://redux.js.org/recipes/usage-with-typescript): Examples of how to declare types for actions, reducers, and the store
226+
- [Redux Toolkit docs: Advanced Tutorial](https://redux-toolkit.js.org/tutorials/advanced-tutorial): shows how to use RTK and the React-Redux hooks API with TypeScript
227+
- [React+TypeScript Cheatsheet](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet): a comprehensive guide to using React with TypeScript
228+
- [React + Redux in TypeScript Guide](https://github.com/piotrwitek/react-redux-typescript-guide): extensive information on patterns for using React and Redux with TypeScript

website/core/Footer.js

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)