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
Copy file name to clipboardExpand all lines: docs/tutorials/typescript.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,7 @@ export type AppDispatch = typeof store.dispatch
65
65
66
66
### Define Typed Hooks
67
67
68
-
While it's possible to import the `RootState` and `AppDispatch` types into each component, it's **better to create typed versions of the `useDispatch` and `useSelector` hooks for usage in your application**. . This is important for a couple reasons:
68
+
While it's possible to import the `RootState` and `AppDispatch` types into each component, it's **better to create typed versions of the `useDispatch` and `useSelector` hooks for usage in your application**. This is important for a couple reasons:
69
69
70
70
- For `useSelector`, it saves you the need to type `(state:RootState)` every time
71
71
- For `useDispatch`, the default `Dispatch` type does not know about thunks. In order to correctly dispatch thunks, you need to use the specific customized `AppDispatch` type from the store that includes the thunk middleware types, and use that with `useDispatch`. Adding a pre-typed `useDispatch` hook keeps you from forgetting to import `AppDispatch` where it's needed.
Copy file name to clipboardExpand all lines: docs/usage/usage-guide.md
+7-24Lines changed: 7 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Every Redux app needs to configure and create a Redux store. This usually involv
24
24
- Importing or creating the root reducer function
25
25
- Setting up middleware, likely including at least one middleware to handle asynchronous logic
26
26
- Configuring the [Redux DevTools Extension](https://github.com/zalmoxisus/redux-devtools-extension)
27
-
- Possibly altering some of the logic based on whether the application is being built for development or production.
27
+
- Possibly altering some of the logic based on whether the application is being built for development or production
28
28
29
29
### Manual Store Setup
30
30
@@ -66,7 +66,7 @@ This example is readable, but the process isn't always straightforward:
66
66
67
67
`configureStore` helps with those issues by:
68
68
69
-
- Having an options object with "named" parameters, which can be easier to read.
69
+
- Having an options object with "named" parameters, which can be easier to read
70
70
- Letting you provide arrays of middleware and enhancers you want to add to the store, and calling `applyMiddleware` and `compose` for you automatically
71
71
- Enabling the Redux DevTools Extension automatically
72
72
@@ -144,7 +144,7 @@ If you provide the `middleware` argument, `configureStore` will only use whateve
144
144
[Reducers](https://redux.js.org/basics/reducers) are the most important Redux concept. A typical reducer function needs to:
145
145
146
146
- Look at the `type` field of the action object to see how it should respond
147
-
- Update its state immutably, by making copies of the parts of the state that need to change and only modifying those copies.
147
+
- Update its state immutably, by making copies of the parts of the state that need to change and only modifying those copies
148
148
149
149
While you can [use any conditional logic you want](https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-2/#switch-statements) in a reducer, the most common approach is a `switch` statement, because it's a straightforward way to handle multiple possible values for a single field. However, many people don't like switch statements. The Redux docs show an example of [writing a function that acts as a lookup table based on action types](https://redux.js.org/recipes/reducing-boilerplate#generating-reducers), but leave it up to users to customize that function themselves.
The only truly necessary part here is the reducer itself. Consider the other parts:
387
387
388
-
- We could have written the action types as inline strings in both places.
389
-
- The action creators are good, but they're not _required_ to use Redux - a component could skip supplying a `mapDispatch` argument to `connect`, and just call `this.props.dispatch({type : "CREATE_POST", payload : {id : 123, title : "Hello World"}})` itself.
388
+
- We could have written the action types as inline strings in both places
389
+
- The action creators are good, but they're not _required_ to use Redux - a component could skip supplying a `mapDispatch` argument to `connect`, and just call `this.props.dispatch({type : "CREATE_POST", payload : {id : 123, title : "Hello World"}})` itself
390
390
- The only reason we're even writing multiple files is because it's common to separate code by what it does
391
391
392
392
The ["ducks" file structure](https://github.com/erikras/ducks-modular-redux) proposes putting all of your Redux-related logic for a given slice into a single file, like this:
`createSlice` looked at all of the functions that were defined in the `reducers` field, and for every "case reducer" function provided, generates an action creator that uses the name of the reducer as the action type itself. So, the `createPost` reducer became an action type of `"posts/createPost"`, and the `createPost()` action creator will return an action with that type.
Most of the time, you'll want to define a slice, and export its action creators and reducers. The recommended way to do this is using ES6 destructuring and export syntax:
Data fetching logic for Redux typically follows a predictable pattern:
618
601
619
-
- A "start" action is dispatched before the request, to indicate that the request is in progress. This may be used to track loading state to allow skipping duplicate requests or show loading indicators in the UI.
602
+
- A "start" action is dispatched before the request to indicate that the request is in progress. This may be used to track loading state, to allow skipping duplicate requests, or show loading indicators in the UI.
620
603
- The async request is made
621
604
- Depending on the request result, the async logic dispatches either a "success" action containing the result data, or a "failure" action containing error details. The reducer logic clears the loading state in both cases, and either processes the result data from the success case, or stores the error value for potential display.
622
605
@@ -1037,7 +1020,7 @@ One of the core usage principles for Redux is that [you should not put non-seria
1037
1020
1038
1021
However, like most rules, there are exceptions. There may be occasions when you have to deal with actions that need to accept non-serializable data. This should be done very rarely and only if necessary, and these non-serializable payloads shouldn't ever make it into your application state through a reducer.
1039
1022
1040
-
The [serializability dev check middleware](../api/getDefaultMiddleware.mdx) will automatically warn anytime it detects non-serializable values in your actions or state. We encourage you to leave this middleware active to help avoid accidentally making mistakes. However, if you _do_ need to turnoff those warnings, you can customize the middleware by configuring it to ignore specific action types, or fields in actions and state:
1023
+
The [serializability dev check middleware](../api/serializabilityMiddleware.mdx) will automatically warn anytime it detects non-serializable values in your actions or state. We encourage you to leave this middleware active to help avoid accidentally making mistakes. However, if you _do_ need to turnoff those warnings, you can customize the middleware by configuring it to ignore specific action types, or fields in actions and state:
0 commit comments