-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DRAFT] Revamp "Redux Essentials" tutorial to be TS-first and update contents #4706
base: master
Are you sure you want to change the base?
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
✅ Deploy Preview for redux-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
d107755
to
36dfbeb
Compare
b638fb2
to
8aa8994
Compare
```ts title="features/posts/postsSlice.ts" | ||
// highlight-start | ||
// Import the `PayloadAction` TS type | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { createSlice, PayloadAction } from '@reduxjs/toolkit' | |
import type { PayloadAction } from '@reduxjs/toolkit' | |
import { createSlice } from '@reduxjs/toolkit' |
Explicit type imports could help better distinguish between TS/JS related material on a quick glance.
import { postAdded } from './postsSlice' | ||
import { useAppDispatch } from '@/app/hooks' | ||
|
||
import { type Post, postAdded } from './postsSlice' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { type Post, postAdded } from './postsSlice' | |
import type { Post } from './postsSlice' | |
import { postAdded } from './postsSlice' |
More of a personal nitpick, feel free to ignore :)
|
||
Once we have the `dispatch` function available in our component, we can call `dispatch(postAdded())` in a click handler. We can take the title and content values from our React component `useState` hooks, generate a new ID, and put them together into a new post object that we pass to `postAdded()`. | ||
Once we have the `dispatch` function available in our component, we can call `dispatch(postAdded())` in a click handler. We can take the title and content values from our form, generate a new ID, and put them together into a new post object that we pass to `postAdded()`. | ||
|
||
```jsx title="features/posts/AddPostForm" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```jsx title="features/posts/AddPostForm" | |
```tsx title="features/posts/AddPostForm" |
Not 100% sure if this should be tsx
, but thought I'd mention it just in case.
```js title="features/posts/postsSlice.js" | ||
```ts title="features/posts/postsSlice.ts" | ||
// highlight-next-line | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { createSlice, PayloadAction } from '@reduxjs/toolkit' | |
import type { PayloadAction } from '@reduxjs/toolkit' | |
import { createSlice } from '@reduxjs/toolkit' |
```js title="features/users/usersSlice.js" | ||
import { createSlice } from '@reduxjs/toolkit' | ||
```ts title="features/users/usersSlice.ts" | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { createSlice, PayloadAction } from '@reduxjs/toolkit' | |
import type { PayloadAction } from '@reduxjs/toolkit' | |
import { createSlice } from '@reduxjs/toolkit' |
Since we can't just put a `Date` class instance into the Redux store, we'll track the `post.date` value as a timestamp string: | ||
Since we can't just put a `Date` class instance into the Redux store, we'll track the `post.date` value as a timestamp string. We'll add it to the initial state values (using `date-fns` to subtract a few minutes from the current date and time), and also add it to each new post in the prepare callback | ||
|
||
```ts title="features/posts/postsSlice.ts"= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```ts title="features/posts/postsSlice.ts"= | |
```ts title="features/posts/postsSlice.ts" |
```jsx title="features/posts/ReactionButtons.js" | ||
import React from 'react' | ||
```ts | ||
import { createSlice, nanoid, PayloadAction } from '@reduxjs/toolkit' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { createSlice, nanoid, PayloadAction } from '@reduxjs/toolkit' | |
import type { PayloadAction } from '@reduxjs/toolkit' | |
import { createSlice, nanoid } from '@reduxjs/toolkit' |
In this case, our auth state is really just the current logged-in username, and we'll reset it to `null` if they log out. | ||
|
||
```ts title="features/auth/authSlice.ts" | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { createSlice, PayloadAction } from '@reduxjs/toolkit' | |
import type { PayloadAction } from '@reduxjs/toolkit' | |
import { createSlice } from '@reduxjs/toolkit' |
Given that, we can import the `userLoggedOut` action from `authSlice.ts` into `postsSlice.ts`, listen for that action inside of `postsSlice.extraReducers`, and return an empty posts array to reset the posts list on logout: | ||
|
||
```ts title="features/posts/postsSlice.ts" | ||
import { createSlice, nanoid, PayloadAction } from '@reduxjs/toolkit' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { createSlice, nanoid, PayloadAction } from '@reduxjs/toolkit' | |
import type { PayloadAction } from '@reduxjs/toolkit' | |
import { createSlice, nanoid } from '@reduxjs/toolkit' |
Actual content changes for #4393 , at long last!
The current WIP example code is over in:
I'm doing another round of revisions and step-by-step checking to those code commits as I rework the tutorial content, but that should be the progression and code content I want to show off in the tutorial.
Big picture summary:
createListenerMiddleware
and thunks increateSlice