React Redux Thunk Middleware in Redux Toolkit for Async Actions with Axios

Поділитися
Вставка
  • Опубліковано 13 жов 2024

КОМЕНТАРІ • 266

  • @morphine496
    @morphine496 2 роки тому +31

    I'm finishing my third app for my portofolio and this has helped me immensely. I love the way this guy explains it and the quality he brings.

  • @rodinCodin
    @rodinCodin Рік тому +8

    Thanks for your work, Mr. Gray! Im building out front end for one of my bootcamp projects and this is hugely helpful in navigating complex RTK environment!

  • @learner8084
    @learner8084 8 місяців тому +1

    Thanks very much for the video. I'm slowing watching and learning everything you show here. I benefitted greatly from your video. I understand much better now. I have a side question, I find the vertical line marking the block and also the horizontal line on the first line of the block very useful in following your video. May I know what extension or settings that enable this ? Thanks !!!

  • @Kasiux
    @Kasiux 2 роки тому +9

    This is insane! How can you make something that used to be just html files into this?! I mean... It's great, but....

  • @ahmedmusawir
    @ahmedmusawir Рік тому +2

    Awesome stuff! As usual ... quick question, do you think we should include Axios since we have RTK query? Does the RTK query handle caching and everything on its own? I'd appreciate a reply since I'm learning the whole thing from you and there is no better tutorial available on the topic anywhere ... thanx in advance!

    • @Kourosh_ey
      @Kourosh_ey 11 місяців тому

      That is my question too

  • @ОлексійКудряшов

    Thanks a lot for this fantastic explanation of Redux, Dave Gray. Would you please provide some guidance on the following bug: I keep getting every post rendered twice + 2 children with the same key red error.. unable to figure out how to fix this... many thanks))

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +3

      Yes, this is due to the new nature of React 18 strict mode. It only uses strict mode in dev mode. You can remove it OR you can replace the concat() call in this code and instead, create a new array with the spread operator.

  • @prince5922
    @prince5922 2 роки тому +3

    This series is the most exhaustive Redux tutorial on youtube, thank you so much Dave. There are a few observations that I've had while following and I thought I might share them, at 28:15 inside the onSavePostClicked method the catch block in this case won't work because the unwrap method itself doesn't throw an error, we need to have an async function with await to achieve that functionality. Furthermore at, 5:59 and inside the fetchPosts thunk, inside the catch we are returning err.message; however, this won't work because we need to return a promise from the thunk callback and according to the promise result thunk will create and trigger the appropriate actions. Since this is not a promise thunk doesn't know what to do and it can't create and trigger the reject promise action. These are some of my observations. All in all, 'I'm really loving this series.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +4

      Good discussion!
      1) The unwrap method can throw an error. See ( redux.js.org/tutorials/essentials/part-5-async-logic#checking-thunk-results-in-components ) for the same code and a further docs reference here: ( redux-toolkit.js.org/api/createAsyncThunk#unwrapping-result-actions ) ..."The promise returned by the dispatched thunk has an unwrap property which can be called to extract the payload of a fulfilled action _or to throw either the error_ ..."
      2) Correct! 💯 This correction is already annotated for the video in the description and appears at 6:22. This is also already corrected in the provided Github source code.
      Glad you are enjoying the series!

    • @prince5922
      @prince5922 2 роки тому +2

      @@DaveGrayTeachesCode Thank you so much for responding, your reply made my day. Regarding the first point, the code in the docs, the reference of which you've shared, it has an "await" before the dispatch, but at 28:15 in the video, "await" isn't typed before dispatch, which I think is why I was having trouble catching the error.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +3

      @@prince5922 ah I see! Good catch!

  • @willyhorizont8672
    @willyhorizont8672 2 роки тому +3

    Great tutorial Dave thanks, can't wait to see this async thunk with useAxiosPrivate hook you created in the series :)

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +2

      They are similar. Our posts state has the posts, a loading state, and an error just like the custom Axios hooks structure. The extraReducers builder cases respond to the promise status returned where we set the state status. Very similar structures!

    • @willyhorizont8672
      @willyhorizont8672 2 роки тому

      @@DaveGrayTeachesCode so you mean I can use axios interceptor inside extraReducers builder like that?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +2

      Not inside the builder case, but I did put axios inside the fetchPosts async thunk. It could be intercepted there before returning data.

  • @Const-y9q
    @Const-y9q 2 роки тому +6

    I've never seen a tutorial like this. Thank you so much. Dave ☺️

  • @spondoolie6450
    @spondoolie6450 Рік тому +1

    I was having problems where my API for posts was being called twice and giving me 200 posts (100 unique). I disabled strict mode and it solved the problem. I don't know if that was the main culprit or just a bandaid, but after watching this video 3 times to verify it's the only thing I could think of.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +1

      Yes, I made a video about this React 18 change: ua-cam.com/video/81faZzp18NM/v-deo.html

  • @saquibasghar5152
    @saquibasghar5152 Рік тому +1

    Hi Dave at 9:20, you added concat method to post array, when useEffect is called, reactStrict mode will make another GET request and the response will concat with the existing post array with new array, thus post length 200 and every post element is duplicatetd

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +2

      You are correct. This tutorial was made with React 17 before strict mode did that. Use the spread operator to create a new array instead.

    • @saquibasghar5152
      @saquibasghar5152 Рік тому +1

      @@DaveGrayTeachesCode Thanks Dave for your reply, I used the spread operator. Again, thanks for generous contribution in making this kind of tutorial and helping future aspiring react developer in their career.

    • @hozayves
      @hozayves 10 місяців тому +1

      @@saquibasghar5152 Hello friend, I have the same error for duplicated posts, Could you please help me to know how you resolve it.

    • @hozayves
      @hozayves 10 місяців тому

      Problem resolved buddy Thanks a lot @DaveGrayTeachesCode , @saquibasghar5152
      @saquibasghar5152

    • @vickysawant19
      @vickysawant19 10 місяців тому

      @@hozayves // state.posts = state.posts.concat(loadedPosts)
      state.posts = [...loadedPosts];

  • @shineLouisShine
    @shineLouisShine 8 місяців тому

    Thank you, Dave.
    It seems like one thing seperates this video and this entire Redux series from perfection:
    The absence of Redux Saga tutorial.
    If it is not too late to plug in another chapter - that will be more than welcome :)
    (...As well as your insights about the newRedux core 5.0, React-Redux 9.0, Reselect 5.0 {which I know totally nothing of}, and Redux Thunk 3.0....)

  • @samiullahsheikh5015
    @samiullahsheikh5015 2 роки тому +9

    13:40 As usual you are producing high quality content which is super awesome.
    Dave, I am little confuse about dependency array. Can you please explain the following point.
    I know the use of dependency array in useEffect but still didn't get it what is the purpose of puting dispatch in dependence array?
    Usually we put something in dependency array whose value change but dispatch itself is a function.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +10

      Good question, Sami. If using strict mode, you will get warnings about not including it in the console. It doesn't bother anything to be there, but I agree that it may not be necessary either. It was presented this way in the docs example I based the tutorial on, so I left it in there.

    • @codingispower1816
      @codingispower1816 11 місяців тому

      In the postsSlice.js I notice that in the fetchPosts.fulfilled addCase, concatenating the loaded posts is making the posts come back twice. Fixed it by changing the line to state.posts = loadedPosts;@@DaveGrayTeachesCode

  • @codegym2306
    @codegym2306 2 роки тому

    Thank you, Dave, your explanation gave me a more insight of async thunk

  • @yashpandey350
    @yashpandey350 2 роки тому +2

    Thank you Sir for this great tutorial.😍😍😍😍

  • @ikurbano
    @ikurbano 2 роки тому +1

    Hi Dave! Thanks for another great tutorial! I have a question. When you created a new post, you pushed it right away to the array of posts in the state. I think you dicided to go that way because the new post has not been pushed into the database which you get all posts from. Please, correct me if I am wrong. Also, if I am right, if you send (via post method) a new post into the same database which you get all your posts from, you need to dispatch fetchAllPosts again so that the new post show up along with old posts.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      In the extraReducers, the builder.addCase has an addNewPost case. If the promise is fulfilled, the state is updated there. That case responds to the addNewPost async thunk promise status.

  • @dragosgheceanu9847
    @dragosgheceanu9847 2 роки тому

    Was impressed by the HQ of the content. Very professional!

  • @subhashgn1775
    @subhashgn1775 2 роки тому +1

    Thank you for the great tutorial.
    How do we manage multiple Api calls in single component.
    Do we have to maintain separate loading, error and succeed variables for each of them or?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      Good question. The API call is issued by the Async Thunk. If you were dispatching more than one thunk or dispatching the same thunk more than once, each thunk would handle its own API call. The extraReducers builder cases in the slice would respond to the thunks separately as well.

  • @rammehar5531
    @rammehar5531 2 роки тому

    Yeah, I always love to see your's full of knowledgable contents videos, Thanks Dave!! and please make a series of Typescript, NextJs with Redux Toolkit.

  • @mounis
    @mounis 2 роки тому

    Great video Dave, thanks a lot.
    Is this the last video of Redux series?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +1

      Thank you 🙏 and no, several more to come. Busy week this week. Next one, may arrive next week.

  • @cole_mcconnell
    @cole_mcconnell 2 роки тому +1

    Super useful video, thanks so much this makes it so much clearer to me how to setup the architecture around thunks and async code involved with redux states!

  • @jagjens
    @jagjens Рік тому +1

    Great video Dave, learnt alot from this one!

  • @triTube70
    @triTube70 2 роки тому +1

    Thanks Dave for the great videos, I learned a lot. One question, where do you use the first parameter you pass to the createAsyncThunk() function? The 'post/fetchPosts' string. How does ReduxToolkit use it?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +6

      Great question! The docs say "A string that will be used as the prefix for the generated action types". It is how Redux identifies the action, and you can see this in Redux Devtools. If we dispatch(fetchPosts()), you will see a 'posts/fetchPosts/pending' first and then either similar with fulfilled or rejected. I hope that helps!

    • @danieljing9319
      @danieljing9319 2 роки тому +2

      First parameter is a string that will be used to generate additional Redux action type constants, representing the lifecycle of an async request:
      For example, a type argument of 'users/requestStatus' will generate these action types:
      pending: 'users/requestStatus/pending'
      fulfilled: 'users/requestStatus/fulfilled'
      rejected: 'users/requestStatus/rejected'

  • @stevemcqueen2887
    @stevemcqueen2887 Рік тому

    You should describe what a Thunk is more. I had to do some additional reading, and I'm watching youtube because I don't like to read 🤣 Apart from that, excellent course again Dave!

  • @harag9
    @harag9 10 місяців тому

    Great tutorial, much appreciated, and well explained. Thanks.

  • @nguyenanh-vt4jv
    @nguyenanh-vt4jv 6 місяців тому

    Thank you Dave, hope you have a video for redux saga

  • @sahilatahar
    @sahilatahar 10 місяців тому

    I regret why I didn't see your tutorial before I wasted my one 🕜 year 😢. Thank you Mr. Dave gray 😊

  • @ebukachuqz
    @ebukachuqz Рік тому

    Hi Dave, please can I get your opinion on this?
    For a real big project which would you work with? Redux with redux thunk or Redux-toolkit?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +1

      Hello - modern Redux is Redux Toolkit. This tutorial shows the using Redux Toolkit to create async thunks. This should meet your needs. As you progress in this series, you will also learn about RTK Query.

  • @jp-sc5du
    @jp-sc5du 2 роки тому

    Hi Dave! I just came from your useAxios video. I'm curious if we can use the useAxios hook with createAsyncThunk and RTK in general? Let's say we put the dispatch call inside useAxios. I really like the reusability of custom hooks. I hope you can give answer as I have just started learning Redux and RTK and would love your guidance. Thank you!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      I know you can use Axios with async thunks and RTK in general. I haven't tried to use a custom useAxios hook with it. The docs have a recipe for an Axios baseQuery: redux-toolkit.js.org/rtk-query/usage/customizing-queries#axios-basequery

  • @alefe238
    @alefe238 2 роки тому +1

    I was following along with the video and noticed that my store duplicated the data that comes from the api. Any idea of why this is happening? Incredible video btw

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      Thanks! Yes, the duplication is due to React 18 strict mode and the use of concat. This tutorial was created with React 17. Other comments on this as well. Create a new array with the spread operator instead of concat.

  • @Gerald-uk5up
    @Gerald-uk5up Місяць тому

    great content learnt alot
    I have a question how do you add a thunk to middleware
    without getting an error
    when I do this middleware: [thunk] I get an error

  • @mihirsharma3857
    @mihirsharma3857 Рік тому

    Thanks Dave, For awesome playlist 🙏

  • @leminhdung1981
    @leminhdung1981 Рік тому +2

    Thanks!

  • @fabianceballos6263
    @fabianceballos6263 2 роки тому +4

    Hi Dave, yours tutorials and skills are amazing, I followed this one but for some reason I'm getting twice the posts and then getting the warning of duplicate key. I downloaded the code and copy paste all the components and still happens. I was wondering if it is about react version (react v18 and react-redux v8).

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +2

      Thank you for the kind words, Fabian! I think it must be React 18. If you are using strict mode v18 renders twice.

    • @moretimeproductions
      @moretimeproductions 2 роки тому +5

      Had the problem as well, I solved it by instead of .concat the state.posts with the loadedPosts just do state.posts = loadedPosts

    • @DGAINEprod
      @DGAINEprod 2 роки тому

      From StackOverflow:
      React StrictMode calls all Effects twice to make sure their cleanup/unmount handlers work as intended. You may need to change your effects accordingly, even if they have an empty dependency list and would normally not unmount before the site is closed.
      Note, this only happens in Strict + development mode. In a production build, effects will only be called once and when their dependencies change.

    • @DGAINEprod
      @DGAINEprod 2 роки тому

      For additionnal info, consult the React documentation page "Ensuring reusable state"

    • @hozayves
      @hozayves 10 місяців тому +1

      @@moretimeproductions I had the same problem as well for 5 days but now I solved.
      Thanks a lot buddy

  • @Lotpite
    @Lotpite Рік тому

    really easy to undestand, thank for nice explanation

  • @mayanxoni
    @mayanxoni Рік тому

    One thing worth mentioning at the end of the video is that when the new posts are added one after another, we get the following warning:
    Warning: Encountered two children with the same key, `101`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted - the behavior is unsupported and could change in a future version.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      Note that this only occurs because the jsonplaceholder website doesn't really add the new posts. I have updated the course resources code to better handle this, but it would not be an issue if not for the jsonplaceholder site behavior.

    • @mayanxoni
      @mayanxoni Рік тому

      @@DaveGrayTeachesCode thanks for the reply! I'll check it out.

  • @hosamgnaba3205
    @hosamgnaba3205 2 роки тому

    a very fat big like from me man, I really appreciate what you do here ? thank you

  • @MartinPerez-mi1ty
    @MartinPerez-mi1ty Рік тому

    Thank you so much this was so helpful, greetings from Peru

  • @mohamedyoussef8835
    @mohamedyoussef8835 2 роки тому +1

    Best tutorial on UA-cam ... but you should not use try/catch in createAsyncThunk or .addCase(fetchPosts.rejected, will never be called. Just let it throw the error:
    const fetchPosts = createAsyncThunk('posts/fetchPosts', async () => {
    const {data} = await axios.get('/posts')
    return data
    })

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      Thank you for the kind words, Mohamed 🙏 and great catch! My try-catch habit got in the way here. You are correct, and I have updated the source code repos. 💯🚀

  • @maxwellalexander6474
    @maxwellalexander6474 Рік тому +1

    ¡Gracias!

  • @fithamlakfikrie4657
    @fithamlakfikrie4657 Рік тому

    Thank you for this clear explanation!

  • @omi7868
    @omi7868 Рік тому

    thank you, Dave Gray. Much appreciated.

  • @lutfisken
    @lutfisken Рік тому

    Great video thanks.
    Still I think when looking in our own application with custom (simple in my opinion) implemenation of Redux (actions in one folder and reducers in one folder), where we are now thinking refactoring to Redux Toolkit, the whole createSlice() syntax seem to quickly get messy and hard read with one huge object as parameter where properties like "reducer" and "prepare", "extraReducers" etc in turn have there curly brackets and object.....Hope you see what I mean, kind of riskt to end up in spaghetti-syntax if not being careful .
    Still I will look in to it as I understand Redux Toolkit will be the standard way of setting up Redux.

  • @wzup23
    @wzup23 10 місяців тому

    Dude, thank you for this!

  • @dieuhuyen0812
    @dieuhuyen0812 2 роки тому

    Thankyou teacher. The tutorials are very good!

  • @maxwellalexander6474
    @maxwellalexander6474 Рік тому

    Great content. Its helping me a lot to learn redux. Thanks so much.

  • @karlo768
    @karlo768 2 роки тому

    Thank you so much, I've learned a lot from these videos

  • @micharozek3046
    @micharozek3046 2 роки тому +1

    Man you're talking the same as Hershel Greene from the Walking Dead xD Thx for the vid! :)

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +2

      I hadn't heard that comparison before! I'm nowhere near that old, but Hershel was great on that show 💯

  • @jkkim6363
    @jkkim6363 2 роки тому

    Hi Dave!
    First of all, really thanks for this amazing tutorial! :)
    And I followed this one, everything works well.
    But I have a question,
    When I add a bunch of new posts, that are more than two post,

    I found that they all have an id of '101'.
    so I saw the message "Encountered two children with the same key, `101` in the console.
    and I also found another related problem, when I clicked the reaction buttons, the buttons' response has a problem too.
    Could you check these problems?
    How can I solve this problem? How should I change the code?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      Hello Kris - you can follow the link in the description to the resources for this Redux series and view my source code for lesson 3. Compare to yours for differences.

  • @garikmelqonyan9130
    @garikmelqonyan9130 Рік тому

    Great tutorial, Dave.
    Thanks a lot

  • @darklord11269
    @darklord11269 Місяць тому

    thank you, this video is helpful for me

  • @annusingh4694
    @annusingh4694 2 роки тому

    Hi Dave! Thanks for this helpful video. Can you please tell how to paginate the posts which we get after fetchPosts async thunk?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      You're welcome! I have a Pagination tutorial here: ua-cam.com/video/9ZbdwL5NSuQ/v-deo.html

    • @annusingh4694
      @annusingh4694 2 роки тому

      @@DaveGrayTeachesCode Awesome! Thank you.

  • @ema_devolder
    @ema_devolder 9 місяців тому

    A very above explanation...

  • @tzCraig
    @tzCraig Рік тому

    Thanks, Dave! You're saving my life here... #codingBootcamp 😰

  • @techcheck3042
    @techcheck3042 Рік тому

    Great tutorial! Thank you!

  • @goodnewsjohn2482
    @goodnewsjohn2482 Рік тому

    You've earned a new subscriber

  • @viditgoel2909
    @viditgoel2909 11 місяців тому

    thanks for this amazing tutorial

  • @VietTranIT
    @VietTranIT Рік тому

    Thanks, appreciate the excellent tutorial

  • @wasimdorboz
    @wasimdorboz 2 роки тому

    you best teacher in the world 😁😁

  • @TheSesameStreetGang
    @TheSesameStreetGang 2 роки тому

    Great video as always but why would you not create a seperate reactions object and add that everywhere it is needed? It would dry up your code a lil!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      The reactions data is an object, but I may be missing your suggestion here? Are you thinking a separate slice for reactions? That would create more code. I kept them with the posts state because they are part of the posts. In addition, I based this on the official docs example (gave credit at beginning of video) and you can reference that structure here: redux.js.org/tutorials/essentials/part-5-async-logic

    • @TheSesameStreetGang
      @TheSesameStreetGang 2 роки тому

      @@DaveGrayTeachesCode Hey, I just meant like an object lookup like this:
      const reactions = {
      thumbsUp: 0,
      wow: 0,
      heart: 0,
      rocket: 0,
      coffee: 0,
      };
      At the top and then whenever they are needed within the slice you can just do something like:
      prepare(title, body, userId) {
      return {
      payload: {
      id: nanoid(),
      title,
      body,
      userId,
      reactions,
      date: new Date().toISOString(),
      },
      or
      const loadedPosts = action.payload.map((post) => {
      post.date = sub(new Date(), { minutes: min++ }).toISOString();
      post.reactions = reactions;
      return post;
      });
      Since we're using the same object in multiple places, if we want to make a change we can just make it to the first object and it will change in multiple places in the slice.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      @@TheSesameStreetGang I see what you're saying 🙏 Yes, this would work where I had to populate the data that was not really in the fake API 💯

  • @dilucsuki8470
    @dilucsuki8470 Рік тому

    20:40 even tho the canSave looks better it's not as good as it was, since it creates another execution context, would be perfect with just
    title && content && userId && addRequestStatus==="idle";
    :D lovely tutorial + loved the extra POST example

  • @jiweihe3413
    @jiweihe3413 2 роки тому

    Thanks sir, the video is really helpful as always. 7:00 it seems when I include catch error in createasyncthunk(), fetch data is always resolved as Success. Only works by removing the catch inside createasyncthunk.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +1

      You are correct! 💯 The source code in the course resources for this lesson was updated to reflect this awhile back. See the previous comment from
      mohamed youssef for this video. YT has recently introduced a feature where I can note "corrections". Thank you for the reminder - I will add this now correction note now 🚀

    • @jiweihe3413
      @jiweihe3413 2 роки тому

      @@DaveGrayTeachesCode Thank you!

  • @빵실한피글렛
    @빵실한피글렛 2 роки тому

    This video is very helpfull for me, thanks a lot!!

  • @SanjarKodirov-ww3le
    @SanjarKodirov-ww3le Рік тому

    awesome lessons, thank you

  • @suryasaini1844
    @suryasaini1844 Рік тому

    Hi Dave, Thanks for the tutorial. I have a very sillly question. Why we are not using addNewPost.pending and addNewPost.rejected actions inside extra reducers Similary for fetchUsers we are not using the same pattern.

  • @muhammadnurali4347
    @muhammadnurali4347 2 роки тому

    Hello, thank you for the great tutorial. After you called store.dispatch(fetchUsers()) in index.js, the AddPostForm component can access users state via useSelector, but when i checked the redux global state with redux devtools, there is only posts state. How came AddPostForm component access users state without it being saved in global state ?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      You may want to compare your code to my course resource code for lesson 3 as the users state is being saved. In the index.js, the fetchUsers function is imported from the usersSlice and dispatched with store.dispatch(fetchUsers())

  • @caraxes
    @caraxes 2 роки тому

    Hi great video. When you have in plan something about redux saga with slice's?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +4

      I need to get through the redux toolkit basics first 🚀

    • @DeepakGupta-pz4fx
      @DeepakGupta-pz4fx 2 роки тому

      @@DaveGrayTeachesCode redux saga using fetch api data implementation

  • @yt-sh
    @yt-sh 2 роки тому

    Hello Dave, awesome video.
    Can we get RTK Query next?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +4

      That is coming soon! 💯🚀

    • @yt-sh
      @yt-sh 2 роки тому

      @@DaveGrayTeachesCode Awesome, really appreciate it!
      Btw I tried but couldnt understand, can you tell me what is the core reason for the usage of builder in RTK?

  • @hardwired66
    @hardwired66 2 роки тому

    thanks you for advance react js tutorial sir

  • @jritzeku
    @jritzeku Рік тому

    Would this redux code/structure be same once we incorporate a backend like Express js or even Firebase? For instance, I've seen API calls generally be made in a component like PostList or else in a custom hook like useFetchCollection. However in this example, we are handling such tasks in our slices, which then makes lot of the uses cases for custom hooks not really worth it anymore.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +1

      This is one way to do it. If you keep going in this series, you will see an RTK Query implementation that generates its own hooks, too.

    • @jritzeku
      @jritzeku Рік тому

      @@DaveGrayTeachesCode IS there one you prefer/recommend? Im assuming using RTK query would be preferred since it offers performance benefits. Man...redux jus tkeeps changing haha.

  • @moretimeproductions
    @moretimeproductions 2 роки тому

    First of all thanks for this great tutorial.
    I am having a problem towards the end. When I add the payload in AddPostForm to dispatch(addNewPost({ title, body: content, userId })) I get an error that says "Expected 0 arguments, but got 1.", which is strange, because the action should expect the payload as an argument right?
    I did the tutorial in TypeScript to practise that, but I don't think that the problem is with that. Do you have any idea, why that is?
    Also when adding a new post whe should add an id at action.payload.id = nanoid(); or else if you add more than one post, they will all have an id of 101, which will cause problems.

    • @moretimeproductions
      @moretimeproductions 2 роки тому +1

      Ok, maybe that was TypeScript after all. For anybody, who might have that problem as well, here is how I solved it: We need a payloadCreator.
      so when we createAsyncThunk for addNewPost, we pass in the "initialPost". This needs a structure, so I set up an
      interface PostToAdd {
      title: string;
      body: string;
      userId: number;
      }
      and then when we pass the initialPost argument, add that type "async(initialPost: PostToAdd).
      That solved it for me.
      Thanks again for the great tutorial!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +1

      @@moretimeproductions glad you worked it out! 💯

    • @stevereid636
      @stevereid636 2 роки тому +1

      @@moretimeproductions Thanks! This worked for e as well 👍🏾

  • @yogendra591
    @yogendra591 Рік тому

    Any idea, do we need to test the createAsyncThunk separately while testing the createSlice in Redux toolkit?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      This may help: stackoverflow.com/questions/67087596/jest-testing-createasyncthunk

  • @salihhasan9662
    @salihhasan9662 2 роки тому

    thank you Dave you are really help me so match

  • @iedit1713
    @iedit1713 2 роки тому

    good video.... but my when i fetch from api i have been fetching the same content twice some how?? what might be the possible error here? is the key in PostsExcerpt possible error here? that only happens when i use ordered post...with normal post.map() i am not fetching the multiple data

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +2

      If you are using React 18 strict mode, you might experience this. There is an array.concat() in there that doubles the results with the new strict mode behavior.

  • @RahulSharma-wh8sq
    @RahulSharma-wh8sq Рік тому

    Great content really impressive thanks a lot🙏

  • @sonamohialdin3376
    @sonamohialdin3376 2 роки тому

    Awesome tutorial

  • @andreyvasilenko2011
    @andreyvasilenko2011 2 роки тому

    Hi Dave! Thanks for great tutorial. One question.
    If I update react and react-redux to the current version ("react": "^18.2.0", "react-redux": "^8.0.2"), "posts/fetchPosts/fulfilled" happens twice. How to fix it?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +6

      Quick way: Remove strict mode from the index.js. Your issue only happens because the new React 18 strict mode mounts components twice. This only happens in development mode with strict mode enabled.

  • @joaoalegria7578
    @joaoalegria7578 2 роки тому

    Can this be implemented ina Next.js project normally or do I need to use the Next js wrapper?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому

      I am planning to cover Next.js soon. Next.js is server-side.

    • @joaoalegria7578
      @joaoalegria7578 2 роки тому

      @@DaveGrayTeachesCode Awesome! Yeah I know, I'm currently building an application in react next.js and redux tool kit, that's why I asked. lol I've never used next.js and redux tool kit so I'm still pretty green. Just subbed and my notifications are on! Cheers

  • @okumorphilip8087
    @okumorphilip8087 Рік тому

    Hello Dave, i hope this meets you well, I'm having a little challenge with this chapter, when i fetch my data from the typicode API, each post appears twice, and the top post is always inactive, because, when i use a reaction button on the one on top of the similar post, the reacton count does not respond on the first post, but the reaction counts are updated for the second one and not the first one, when i add a new post, it always goes 2nd, because like i said the first post of the similar post that loads with the app when it starts, looks inactive and doesnt even repond to any clicks

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +2

      Getting duplicate posts is likely due to the concat() in the store.js - This tutorial used React 17 but now, with React 18 in dev mode, each component is mounted twice. Instead of concat, create a new array with brackets and spread in the values [...values]

    • @okumorphilip8087
      @okumorphilip8087 Рік тому +1

      @@DaveGrayTeachesCode Oh, Bless You Dave. Thanks.. When i land my first job, i will buy you Pizza.

  • @danieljing9319
    @danieljing9319 2 роки тому

    informative and clear

  • @iUniverse
    @iUniverse Рік тому +1

    Thank you very much Dave for this tutorial. I have some query though. In the case, fetchPosts.fulfilled, during execution, with this line of code: state.posts = state.posts.concat(loadedPosts) the data is displayed twice hence this warning occured: "react-dom.development.js:86 Warning: Encountered two children with the same key, `1`. Keys should be unique so that...", however, if I changed state.posts = state.posts.concat(loadedPosts) into state.posts = action.payload it displayed okay but I wonder the Time ago and and Reaction also works.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      Due to this tutorial was with React 17 and not React 18's double mount behavior in strict mode. Instead of concat, create a new array with the spread operator. See other comments on this.

  • @yashwanthsrinivas
    @yashwanthsrinivas Рік тому

    nice video. thanks

  • @ModernProjectManager
    @ModernProjectManager Рік тому

    Could you make an Lit Webcomponents + Redux-Thunk. I use it, and could help if you need it.

  • @dharmendrasharma1445
    @dharmendrasharma1445 2 роки тому

    Hi Dave, thanks for great video, in React 18 , API is calling twice so we are getting one error ' Encountered two children with the same key, ' it's not happening in React 17

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +2

      It is happening in React 18 with Strict Mode enabled. The new strict mode mounts, unmounts, and then mounts components again. This will be from the useEffect in PostsList.js in this lesson. Discussing this change and others from React 18 here: ua-cam.com/video/N41B_SVdzwg/v-deo.html ...A solution is to keep moving on in this series until you eventually switch over to RTK Query that removes this type of logic from useEffect.

    • @dharmendrasharma1445
      @dharmendrasharma1445 2 роки тому

      Thanks, Dave Gray for your support

  • @ericmomoh7927
    @ericmomoh7927 2 роки тому

    Nice content and teaching. Please help us how to connect it to firebase firestore and get data from firestore using redux thunk. Thanks for the tutorial it amazing

  • @ougininja8412
    @ougininja8412 Рік тому

    Amazing video

  • @daryazat8049
    @daryazat8049 2 роки тому

    Hi Dave, thank you for a nice explanation! I tried to add this logic to my project, which worked well, the only thing is, I think you have a mistake when fetching all data. Im not sure why you use concat method there? I think you want to add new data to an empty array, but this can go wrong as it did it my case, I had duplicated data in the array. Im not sure why, but by default fetchCountries.fulfilled case runs twice and there you get the data duplicated.. Either just assign loadedData to the state without concat or you need to put an additional if statement to check for the status so it doesnt run twice.. Cheers

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +1

      Hi Darya - there was no problem with the code in React 17 when this tutorial was made. You now have an issue with React 18 Strict Mode as it mounts each component twice. Your fix should work as you noted. You don't want to concat twice. I cover the React 18 strict mode change here: ua-cam.com/video/81faZzp18NM/v-deo.html

    • @daryazat8049
      @daryazat8049 2 роки тому

      @@DaveGrayTeachesCode great, thank you for your advise, I will check this out-)

  • @samislam2746
    @samislam2746 2 роки тому

    I have a question, I saw you wrote dispatch inside the useEffect() array at line 17 inside react_redux_toolkit/03_lesson/src/features/posts/PostsList.js
    `
    useEffect(() => {
    if (postStatus === 'idle') {
    dispatch(fetchPosts())
    }
    }, [postStatus, dispatch])
    `
    what does that mean? isn't dispatch a function? how React is going to know that the dispatch function was changed? it doesn't change, it fires. What If I removed that dispatch from here? what are the downsides?
    And thank you 😁

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +1

      No real downsides I can see other than a possible warning in the console. 💯

    • @samislam2746
      @samislam2746 2 роки тому

      @@DaveGrayTeachesCode 👍

  • @kamelkamatchu8830
    @kamelkamatchu8830 2 роки тому +2

    thank you for this great tutorial but if you run this in react 18 will get an 100 warning of two children with same key and also blogs are dublicated in page but in react 17 there is no error

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 роки тому +1

      React 18 has made some changes. One is rendering twice in strict mode. Not sure this would be the cause but thought of that first.

    • @moretimeproductions
      @moretimeproductions 2 роки тому +2

      Had the problem as well, I solved it by instead of .concat the state.posts with the loadedPosts just do state.posts = loadedPosts

    • @kamelkamatchu8830
      @kamelkamatchu8830 2 роки тому +1

      @@moretimeproductions thanks for your comment i also solve it with hook useRef

    • @moretimeproductions
      @moretimeproductions 2 роки тому +1

      @@kamelkamatchu8830 How did you approach that?

    • @kamelkamatchu8830
      @kamelkamatchu8830 2 роки тому +1

      @@moretimeproductions youtube always delete my comment with link .. and as Mr Dave comment react 18 render two times in strict mode and react team already propose solutions for this

  • @baselinereviews
    @baselinereviews Рік тому

    I have a question, but this may be due to react 18 rather than a redux issue. I wanted to have the posts load when the component was loaded, so I moved the dispatch statement to the component itself and put the dispatch statement inside of a useEffect. However, I noticed that when I did this that now the redux action fires twice. Upon some more reading, this is because react now unmounts and remounts the component to help devs look out for remountable components when in dev mode. If I remove the React.strictMode tags, the redux action no longer fires twice. Is there a way to prevent this behavior? Or is this why in your videos you load the state upon loading the application?

    • @baselinereviews
      @baselinereviews Рік тому

      Ok I found an answer to this and it would be, upon the cleanup of a component, if the promise returned from the dispatch() function is not yet fulfilled, call promise.abort() to abort the calling of the thunk, however, I feel that there has to be a better way

  • @antonioscaravilli9966
    @antonioscaravilli9966 2 роки тому

    I will love to watch this video

  • @SL4UGHT3R
    @SL4UGHT3R Рік тому

    13:50 why dispatch is a dependency in useEffect dependency array?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      As recently as React 17, you would receive a warning if it wasn't. It is a function and likely does not change. With React 18 you should not receive a warning if left out.

    • @SL4UGHT3R
      @SL4UGHT3R Рік тому

      @@DaveGrayTeachesCode ok, thanks!!

  • @razmiqayelyan1497
    @razmiqayelyan1497 2 роки тому

    Thanks a lot

  • @dharmeshgohil9375
    @dharmeshgohil9375 2 роки тому

    Awesome👍👍👍😊

  • @prakashbanjade4374
    @prakashbanjade4374 Рік тому

    As I refresh the page when I am in SinglePost (say /posts/5), I get error as post not found (my custom error if post not found by id). Why?
    Other things work fine. I can navigate to specific post as I click on visit post but refreshing this page is causing error.
    I tried logging out the posts in SinglePost component and it is empty

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      This is a single page app (SPA) as all React apps are. When you refresh the page, you lose your client-side state. It's like restarting an app.

  • @CarlosSuarez-mc3is
    @CarlosSuarez-mc3is Рік тому

    For some reason my extra reducer is not catching the error. I tested error handling and the case for rejected never gets trigger. Looking at the thunk function, I can see that it does catch the error. I'm not sure what could be wrong here. Could you advise what could be the potential issuel

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому +1

      I can only guess from here. Compare your code to mine for differences. The source code is available at the course resources link in the description.

    • @CarlosSuarez-mc3is
      @CarlosSuarez-mc3is Рік тому

      @@DaveGrayTeachesCode sure, but I also comprare it to the documentation and nothing seems different. I might have added a Middleware km the rootreducer, but how would this change the hook? :O

  • @roberainsarmu8135
    @roberainsarmu8135 Рік тому

    I am trouble in getting the source code for code debugging. please can u post github link on description? thank u

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Рік тому

      There is a link to the course resources which includes source code in the description. On the course resources page, each chapter of this series is linked to with source code available.

  • @jowdelgado6865
    @jowdelgado6865 2 роки тому

    So much coding required when redux is used. But this is a good demo video

  • @Xtapodi27
    @Xtapodi27 2 роки тому

    What is your color theme please?

  • @mohamedyoussef8835
    @mohamedyoussef8835 2 роки тому

    Awesome Tutorial +++++++++++++++++++++++