Part 7 of The MERN Stack Project Series - This video shows how to build React form components to complete CRUD operations in our MERN Stack Project. We are using Redux and RTK Query so this video also shows how these CRUD operations are state mutations for Redux. This tutorial is not for beginners. If you are a beginner, check out my full courses all in one playlist here: ua-cam.com/play/PL0Zuz27SZ-6M1Uopt6_VL3gf3cpMnwavm.html
@@MrPindro Yes it does. The only issue since this tutorial was published nearly a year ago is that the Mongoose sequence package does not work with the latest version of Mongoose. This has been discussed in these comments and in my discord forum. You can roll back to the version of Mongoose that I list in my package.json in the course resources, or you can find an equivalent dependency to Mongoose sequence. Some are listed in both the comments and in my discord.
Hello Dave, I haven't bother you in a while, so here I am again 😎 at 19:44 we used the ROLES key for the option text, and that's ok since both keys and values are identical, but just in case we want to localize this app I think we can use ROLES[role] Thanks for your continued efforts to make things easier for us
I experimented with react-query before I took this tutorial. I will be using RTK Query from now on. Thanks to you, Dave. You have explained the use of RTK Query very well. Thanks again. {2023-02-28}, {2023-03-01}
I've finally understood RTK Query thanks to this video and to your Redux-Toolkit playlist. Thank you very very much for your great work!! I hope you reach 500k subs or more before the year ends!!🥳🥳🥳
Hey Dave, loving the content. Any plans on releasing a NEXT Js series? There’s a lot of talk about the T3 stack which utilizes Next, Prisma for database, and tRPC over React Query. Would absolutely love if you could try to do that, you are one of the best teachers on here by far!
For some reason at 43:19, after implementing the prefetch component, the data stays on the edit form(which is expected) but after submitting the edited user and going back to the usersList, the edited user doesn't update until I refresh the page again. This was working previously but now I cant figure out what seems to be the problem here??
Another great tutorial DAVE! I've learned soooo much from your tutorials! I am running into an error at the end of lesson 6 though. I've triple checked and my code is identical to the repository. I get a cors error when my client is attempting to get the users from the server. I was able to solve this by bypassing the cors options and using app.use(cors()) instead in the server file, which I believe allows the client to fetch the data from any url. Could someone please help me understand what could be causing this? Thanks in advance!
It depends on what you need the hook to return if you write a custom hook. If not a custom hook, the documentation tells you what the built-in hook returns.
Hi Dave, I love your videos. I have a question... How can we implement a debounce strategy to an input of type text using RTK Query? I hope you answer, thanks anyway.
This would make a good tutorial for not only a useDebounce hook but also for a search input that automatically sends a request to the API when the input changes. Thank you for the request! 💯
Thanks for the awesome tutorial, I just have a quick question: Is there any reason for someone to want use useLazyGetNotesQuery or useLazyGetUsersQuery
The "lazy" versions provide "manual control over when the data fetching occurs". according to the docs. This means you would use them when you don't want to trigger the query when the component loads.
keepUnusedData = The amount of time in seconds that RTK Query will keep the data available AFTER no components are subscribed to the data. Components unsubscribe when they unmount. polling interval = The amount of time in seconds that RTK Query waits until it automatically sends another request for that query endpoint. This is important when others may be updating the data - like other employees in this example - and you need to see and work with the most recent data instead of stale data from the cache.
This is a follow-up to a comment on another thread where I ask about RTK & selectors created by getSelectors as done in the features slices. My problem is getting the selectors (or state, really) to "hydrate". I realize this can't happen unless the "get" builder query is invoked for the corresponding feature. But how do I force this to happen? For example if I have a feature called "Areas" and I want to us the "selectAllAreas" selector, I can force it to hydrate state by doing this: import { useGetAreasQuery, selectAllAreas } from "./areasApiSlice" and calling useGetAreasQuery first... By invoking useGetAreasQuery, state is hydrated & the selector "selectAllAreas" works. But from another content type, for example as you populate the User drop down in NewNotesForm using selectAllUsers, how does one "force" the state to hydrate? I must be missing something here. Is it in the course dialogue?
I think you will find the full refactor to RTK Query that comes in a later chapter (video) helpful. The way to get new data - which is what I think you are wanting when you say "how does one force the state to hydrate" - is to invalidate an existing cache so another query takes place. You are correct that a user must be logged in with this app for much of anything to take place. The persist behavior is only for when a page is refreshed or a user navigates away and returns. Keep going with this project in full before trying to modify or build your own around it. I think RTK Query will be helpful for what you are trying to do.
@@DaveGrayTeachesCode OK - I apologize. This is totally on me because I missed the Prefetch.js, skipped it in my application and reviewing the episode again this is what I am looking for. Thx
@@DaveGrayTeachesCode That makes. Thank you so much for the tutorial btw. My only concern is that I dont feel confident about any of the API slices mutations/queries. The syntax and the various names are daunting. If I had to give a suggestion, it would be really nice if you explained more in depth where each of the variables were pulled from for stuff like provideTags, transformResponse, noteAdapter, etc.
Sir, If we are subscribed to the endpoints in the Prefetch component. How come we still query the notes and users in the users and notes lists? Also if we invalidateTags on creating new users and posts, how come we still refresh the queries in the users list and notes list? I’m confused, but I guess that’s part of learning.😅
Hi Breldan - Question 1) The prefetch component will only fetch once. Your data will become stale if you do not fetch any changes. 2) Good question - the reasoning is we want the data refreshed on all staff members web displays. Invalidating the cache does not automatically update the display of any logged in browser - it only impacts the cache (stored in the browser) of the user who made the update. Refreshing at a regular interval in the app will display new data for all workers viewing the notes or users lists. You're right! It is good that you ask questions - it is part of learning! 💯
Hello Dave, my endpoint get multiple objects, means- {notesPerPage:12, notes:[...]} . I am still looking a way, how to handle notesPerPage. Should have do that by myself but I am not quick or great learner. If you hint something or tut some in your next lessons, it will be great. Thank you.
It sounds like you are applying pagination. On the frontend, you can handle the notesPerPage value as you see in my pagination video: ua-cam.com/video/9ZbdwL5NSuQ/v-deo.html ...on the backend, if your frontend app sends the value for notesPerPage as part of the request body, you can destructure that value as you see other values have been received in the controller methods.
@@DaveGrayTeachesCode I think my question is wrong. ---------------------------------------------------------------- transformResponse: responseData => { const loadedNotes = responseData.map(note => { note.id = note._id return note }); return notesAdapter.setAll(initialState, loadedNotes) }, ---------------------------------------------------------------- here loadedNotes store the notes array and set the initialState. but I have also other objects like notesPerPage and several others. If I set the initialState an array rather than object and destruct my desired objects from responseData and add to initialState along with notes array it doesn't work properly. I don't know if I express myself properly. Sometimes I feel angry why people talk in different languages.
Great question! I only abstract state from the component when it is needed globally. If the state is only related to the specific form in the component, I usually keep it there.
It is interesting. I have not read much about it. I like my HTML to be as clean as possible, and this adds a bit more to it - but I do not know enough about it yet to form much of an opinion one way or the other.
@@DaveGrayTeachesCode okay i understand, i thought maybe you are familiar with it, you are doing amazing by the way, your HTML and CSS courses are amazing!! Keep doing the amazing work. 👏
Hii dave i have 6months of experience as react developer. I am requesting you to make one video. How one should understand the complex project (means code already written by some body) recently i moved one of the client location.they just assigning the tasks and they are not going to explain the thing properly. I basically want to know how to debug the project in various places and how to understand the api responses and flow of the project.please i am begging you to make a video on this no one in the youtube made one such video about it.and also my sincere request you to do one react project using complete Antd library video, no one explained Antd any where in the youtube.udey also has no as such course. I am waiting for you reply 😊😊😊 thank in advance
Great tutorial Dave, thank you! I was able to do everything, except adding notes in Postman(didnt know what to add in the JSON format). so unsurprisingly i 'm getting an error that localhost:3500/notes not found. Is there an example for the JSON format?
Hi Dave. Wish you can giving me some tips. I tried implement more with this to add some default state into userAdapter.getInitialState({ currentPage: 0}). How could I update the currentPage value state in getUsers GET method once the api return the value.
I guess i more understanding it now after watched it again. usersApiSlice with adapter only handling the api and created the normalized state. if we want to mutation other state that should create another file like "authSlice" to handle the redux stuff and initialState. Thank you so much.
Part 7 of The MERN Stack Project Series - This video shows how to build React form components to complete CRUD operations in our MERN Stack Project. We are using Redux and RTK Query so this video also shows how these CRUD operations are state mutations for Redux. This tutorial is not for beginners. If you are a beginner, check out my full courses all in one playlist here: ua-cam.com/play/PL0Zuz27SZ-6M1Uopt6_VL3gf3cpMnwavm.html
The createNewNote controller doesn’t work!
@@MrPindro Yes it does. The only issue since this tutorial was published nearly a year ago is that the Mongoose sequence package does not work with the latest version of Mongoose. This has been discussed in these comments and in my discord forum. You can roll back to the version of Mongoose that I list in my package.json in the course resources, or you can find an equivalent dependency to Mongoose sequence. Some are listed in both the comments and in my discord.
Oh!! I get it now. Practically everything else works aside from creating a new note.
Anyway, i appreciate your response! 🤝
Hey Dave, i can’t seem to find a link to the discord convo you previously mentioned. Mind copy pasting for me? The link that is. Thanks in advance!
Btw, i just fixed it by having the mongoose-sequence definition on the Note.js in the models folder completely wiped. 😅 thanks again good lad! 🤝
Hello Dave,
I haven't bother you in a while, so here I am again 😎
at 19:44 we used the ROLES key for the option text, and that's ok since both keys and values are identical,
but just in case we want to localize this app I think we can use ROLES[role]
Thanks for your continued efforts to make things easier for us
Hello Ahmad! Good to hear from you my friend. I hope all is well for you. 🙏💯
@@DaveGrayTeachesCode We have survived so far 😐,
Thank for asking my friend 🤍
I experimented with react-query before I took this tutorial. I will be using RTK Query from now on. Thanks to you, Dave. You have explained the use of RTK Query very well. Thanks again.
{2023-02-28}, {2023-03-01}
Glad it helped!
God level programmer and teacher🤩. Love your content !!
Thank you for the kind words! 🙏🙏
Thank you Dave! All up to date and looking forward to continuing the project in the next video.
Great job! 💯
This course is gold 💯 Beautifully structured and detailed.
Thank you for the kind words, Annu! 🙏
I've finally understood RTK Query thanks to this video and to your Redux-Toolkit playlist. Thank you very very much for your great work!! I hope you reach 500k subs or more before the year ends!!🥳🥳🥳
You are welcome! Looking like I may hit 100k by year end and I'll be happy with that.. but I'll keep going! 💯🚀
This series keep getting better, please keep going!
Thank you! Will do! 🚀
Sir you explain every single details about MERN stack. And i love the way you explain.... Love from fellow coders..
Thank you for the kind words, Adarsh! 💯
You are excellent teacher!!!!
YOU are the BEST teacher, thank YOU so much!!!
You're very welcome! 🙏🙏
Awesome Buddy! Really Love Your Tutorial, Made as Simple as Possible
Glad you liked it!
I love your courses
Thank you! 💯🚀
Thanks so much for this course Dave.
Very welcome!
I really enjoyed this course 😇
I just have a small request if possible please make a tutorial on microservices and the CI/CD pipeline.
Thank you for the request! 💯
Hey Dave, loving the content. Any plans on releasing a NEXT Js series? There’s a lot of talk about the T3 stack which utilizes Next, Prisma for database, and tRPC over React Query.
Would absolutely love if you could try to do that, you are one of the best teachers on here by far!
Yes, I do have plans for a NextJS series - and thanks for the detailed request and kind words! 🙏💯🚀
@@DaveGrayTeachesCode thanks for getting back to me, any idea on when that can be expected Dave?
For some reason at 43:19, after implementing the prefetch component, the data stays on the edit form(which is expected) but after submitting the edited user and going back to the usersList, the edited user doesn't update until I refresh the page again. This was working previously but now I cant figure out what seems to be the problem here??
Hello, same here... Did you find out ?
Amazing tutorial thank you very much
You are welcome!
very very good tutorial thanks :)👍👍👍👍❤❤❤
You're welcome!
Another great tutorial DAVE! I've learned soooo much from your tutorials! I am running into an error at the end of lesson 6 though. I've triple checked and my code is identical to the repository. I get a cors error when my client is attempting to get the users from the server. I was able to solve this by bypassing the cors options and using app.use(cors()) instead in the server file, which I believe allows the client to fetch the data from any url. Could someone please help me understand what could be causing this? Thanks in advance!
Why do you sometimes destructure an imported hook with an array and sometimes as an object? How do you know when to use one or the other?
It depends on what you need the hook to return if you write a custom hook. If not a custom hook, the documentation tells you what the built-in hook returns.
Hi Dave, I love your videos. I have a question... How can we implement a debounce strategy to an input of type text using RTK Query? I hope you answer, thanks anyway.
This would make a good tutorial for not only a useDebounce hook but also for a search input that automatically sends a request to the API when the input changes. Thank you for the request! 💯
@@DaveGrayTeachesCode Coming Soon? I need to know hahaha. #anxiety
@@nicolascohen2465 sometime this year!
Thanks for the awesome tutorial, I just have a quick question: Is there any reason for someone to want use useLazyGetNotesQuery or useLazyGetUsersQuery
The "lazy" versions provide "manual control over when the data fetching occurs". according to the docs. This means you would use them when you don't want to trigger the query when the component loads.
@@DaveGrayTeachesCode thank you so much, you have helped me in ways you can’t imagine I am sincerely grateful to you
Finally ♥️
can you please explain the difference in keepUnusedData and polling?
keepUnusedData = The amount of time in seconds that RTK Query will keep the data available AFTER no components are subscribed to the data. Components unsubscribe when they unmount.
polling interval = The amount of time in seconds that RTK Query waits until it automatically sends another request for that query endpoint. This is important when others may be updating the data - like other employees in this example - and you need to see and work with the most recent data instead of stale data from the cache.
Hello. Thanks for your videos. What is the better way to modify _id to id : in backend or frontend like you did?
Either way works, but I prefer the frontend as shown. It is easy with the transformResponse example.
@@DaveGrayTeachesCode thanks 👍🏻
This is a follow-up to a comment on another thread where I ask about RTK & selectors created by getSelectors as done in the features slices. My problem is getting the selectors (or state, really) to "hydrate". I realize this can't happen unless the "get" builder query is invoked for the corresponding feature. But how do I force this to happen? For example if I have a feature called "Areas" and I want to us the "selectAllAreas" selector, I can force it to hydrate state by doing this: import { useGetAreasQuery, selectAllAreas } from "./areasApiSlice" and calling useGetAreasQuery first... By invoking useGetAreasQuery, state is hydrated & the selector "selectAllAreas" works. But from another content type, for example as you populate the User drop down in NewNotesForm using selectAllUsers, how does one "force" the state to hydrate? I must be missing something here. Is it in the course dialogue?
Maybe this works in your course because the user has to log in first and you "persist" state? Is there a standard pattern to make this occur?
I think you will find the full refactor to RTK Query that comes in a later chapter (video) helpful. The way to get new data - which is what I think you are wanting when you say "how does one force the state to hydrate" - is to invalidate an existing cache so another query takes place. You are correct that a user must be logged in with this app for much of anything to take place. The persist behavior is only for when a page is refreshed or a user navigates away and returns. Keep going with this project in full before trying to modify or build your own around it. I think RTK Query will be helpful for what you are trying to do.
@@DaveGrayTeachesCode OK - I apologize. This is totally on me because I missed the Prefetch.js, skipped it in my application and reviewing the episode again this is what I am looking for. Thx
In RTK, is every GET operations considered query and everything else is mutation?
Not just in RTK, but overall, the other CRUD methods all mutate data. You query to Read - but everything else will change the cached data you have.
@@DaveGrayTeachesCode That makes. Thank you so much for the tutorial btw. My only concern is that I dont feel confident about any of the API slices mutations/queries. The syntax and the various names are daunting. If I had to give a suggestion, it would be really nice if you explained more in depth where each of the variables were pulled from for stuff like provideTags, transformResponse, noteAdapter, etc.
Sir, If we are subscribed to the endpoints in the Prefetch component. How come we still query the notes and users in the users and notes lists?
Also if we invalidateTags on creating new users and posts, how come we still refresh the queries in the users list and notes list?
I’m confused, but I guess that’s part of learning.😅
Hi Breldan - Question 1) The prefetch component will only fetch once. Your data will become stale if you do not fetch any changes. 2) Good question - the reasoning is we want the data refreshed on all staff members web displays. Invalidating the cache does not automatically update the display of any logged in browser - it only impacts the cache (stored in the browser) of the user who made the update. Refreshing at a regular interval in the app will display new data for all workers viewing the notes or users lists.
You're right! It is good that you ask questions - it is part of learning! 💯
Thank you so much 🤗👍👏🙌👐🤝🙏
You are welcome! 💯
Are there more videos in this course ?💖💖
I am building this course right now. New videos on Tuesdays and Fridays.
As of 2023 what are your thoughts on RTK Query vs ReactQuery?
They are both good!
Hello Dave, my endpoint get multiple objects, means- {notesPerPage:12, notes:[...]} . I am still looking a way, how to handle notesPerPage. Should have do that by myself but I am not quick or great learner. If you hint something or tut some in your next lessons, it will be great. Thank you.
It sounds like you are applying pagination. On the frontend, you can handle the notesPerPage value as you see in my pagination video: ua-cam.com/video/9ZbdwL5NSuQ/v-deo.html ...on the backend, if your frontend app sends the value for notesPerPage as part of the request body, you can destructure that value as you see other values have been received in the controller methods.
@@DaveGrayTeachesCode I think my question is wrong.
----------------------------------------------------------------
transformResponse: responseData => {
const loadedNotes = responseData.map(note => {
note.id = note._id
return note
});
return notesAdapter.setAll(initialState, loadedNotes)
},
----------------------------------------------------------------
here loadedNotes store the notes array and set the initialState. but I have also other objects like notesPerPage and several others. If I set the initialState an array rather than object and destruct my desired objects from responseData and add to initialState along with notes array it doesn't work properly.
I don't know if I express myself properly. Sometimes I feel angry why people talk in different languages.
what is your opinion on storing form data in a redux slice
Great question! I only abstract state from the component when it is needed globally. If the state is only related to the specific form in the component, I usually keep it there.
Hey Dave, are you familiar with htmx?
It is interesting. I have not read much about it. I like my HTML to be as clean as possible, and this adds a bit more to it - but I do not know enough about it yet to form much of an opinion one way or the other.
@@DaveGrayTeachesCode okay i understand, i thought maybe you are familiar with it, you are doing amazing by the way, your HTML and CSS courses are amazing!! Keep doing the amazing work. 👏
Hii dave i have 6months of experience as react developer. I am requesting you to make one video. How one should understand the complex project (means code already written by some body) recently i moved one of the client location.they just assigning the tasks and they are not going to explain the thing properly. I basically want to know how to debug the project in various places and how to understand the api responses and flow of the project.please i am begging you to make a video on this no one in the youtube made one such video about it.and also my sincere request you to do one react project using complete Antd library video, no one explained Antd any where in the youtube.udey also has no as such course. I am waiting for you reply 😊😊😊 thank in advance
Thanks for the request! 🙏
You are a great teacher 🙌
You might wanna checkout the new experimental feature in vscode called sticky headers. I found it useful myself. 🤗
Thank you for the kind words! 💯
Great tutorial Dave, thank you! I was able to do everything, except adding notes in Postman(didnt know what to add in the JSON format). so unsurprisingly i 'm getting an error that localhost:3500/notes not found. Is there an example for the JSON format?
Yes, I think you need to go back to lesson 4 in this series to see me do that very thing in a video.
@@DaveGrayTeachesCode I'll do that, thanks!
Hi Dave. Wish you can giving me some tips.
I tried implement more with this to add some default state into userAdapter.getInitialState({ currentPage: 0}).
How could I update the currentPage value state in getUsers GET method once the api return the value.
I guess i more understanding it now after watched it again. usersApiSlice with adapter only handling the api and created the normalized state. if we want to mutation other state that should create another file like "authSlice" to handle the redux stuff and initialState. Thank you so much.
Glad you got it!