+ 18

Fetching data!

I am fetching data and giving it to redux then fetching again with infinite scroll and trying to spread the original data and the new data but it gives me an error? it says data is not defined. Althought I have an initial state object that is given to the reducer with data as an empty array Will be srsly waiting for your answer case FETCH_RECIPE_SUCCESS: return { ...state, data: [...data, action.payload.recipes], searchResults: [], loading: false, error: null, fetchSuccess: true }

5th Jan 2021, 12:39 PM
Hardik
Hardik - avatar
16 Réponses
+ 7
Try to change ...data to ...state.data example of update state.data: const state = { loading: false, data: ['tomato', 'carrot'], error: null } const newState = { ...state, data: [...state.data, 'banana'] }; console.log(newState); /* Object { loading: false, data: ['tomato', 'carrot', 'banana'], error: null } */ https://code.sololearn.com/cDruSCmqSq7w/?ref=app
6th Jan 2021, 3:20 AM
Calviղ
Calviղ - avatar
+ 8
Can you please tell or clarify which error?? And you added a default case where reducer returns the state unchanged?? I am not that much qualified but I believe Burey Raj Chhatrala Calviղ Morpheus they can answer
5th Jan 2021, 12:46 PM
Atul
Atul - avatar
+ 8
Hardik Share your code attempt here. Its always easier for others to share a fix quickly when a code link is attached with the question. Error is correct and to be expected. There is no data defined here. data: [... data(undefined), ...action.payload.recipes]
5th Jan 2021, 2:42 PM
Morpheus
Morpheus - avatar
+ 7
You are using it with redux so you can share your code here it will help you more redux template https://code.sololearn.com/W5E46XL5PvM2/?ref=app react redux template https://code.sololearn.com/WVxfxjZ9av4G/?ref=app now share your information is not sufficient for anyone to help share your code here
5th Jan 2021, 12:42 PM
Jiya
Jiya - avatar
+ 6
Hardik then look at your fetch function something wrong there
5th Jan 2021, 1:05 PM
Jiya
Jiya - avatar
+ 5
I said share your code mostly the erorr comes from api fetch function and you are just showing your reducer function action type case here So its not possible to help you
5th Jan 2021, 12:47 PM
Jiya
Jiya - avatar
+ 5
Morpheus Your answers clears my doubt halfly The reason I don't want to share the code is that I have almost done it and don't want it to show "to everyone" because it somehow makes the code released already. Thank you for your explaination.
6th Jan 2021, 3:31 PM
Hardik
Hardik - avatar
+ 4
Мг. Кнап🌠 thanks for the suggestion but I have already stated the reason I can't share the whole code. But will keep in mind from next time Thank you.
6th Jan 2021, 3:32 PM
Hardik
Hardik - avatar
+ 4
Calviղ Thanks a lot!!!!!!!!!!!!! Yeah exactly!!!! I forget to add "state" before .data That's what makes the error! You got it all right. Thank you again for the example code. Your answer was too much needful🙏 Again thank you!
6th Jan 2021, 3:34 PM
Hardik
Hardik - avatar
+ 3
Jiya I have shared a part of my code Sharing the code will not be more better in my view. The error clearly says that state data "can't be fetch" Just this the error says
5th Jan 2021, 12:55 PM
Hardik
Hardik - avatar
+ 2
Jiya I am fetching the same data again with infinite scroll.. The code you have provided is applying with redux :(
5th Jan 2021, 12:44 PM
Hardik
Hardik - avatar
+ 2
Atul🦚 Firstly Thank you for mentioning your friends or experts. But they aren't ping actually so please have a look again :( And secondly I have an initial state object which's given to the reducer.. editing - thank you for pinging them
5th Jan 2021, 12:52 PM
Hardik
Hardik - avatar
+ 2
As some, have already mentioned, that you need to share more than just the reducer case, we need to know more, what payload are you passing what initial states you have, and what action you are dispatching.. Try to share your code using stackblitz, or codesandbox.. it will be more helpful for us to clarify your doubt
6th Jan 2021, 10:56 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 2
Iam fecting data and giving it to redux then feching again with infinite scroll and trying to spread to original data and new data it gives me an error? it says daa is not defined .although i have an initial state object that is given to the reducer with dataas an empty array will ne seriously waiting for yoir answer
6th Jan 2021, 12:35 PM
Akshaya Bellamkonda
Akshaya Bellamkonda - avatar
+ 2
he
6th Jan 2021, 8:30 PM
فوف آلجنوبية
فوف آلجنوبية - avatar
+ 2
Can plz tell me what is this means which Language or code😁
7th Jan 2021, 5:38 AM
Skys Winner
Skys Winner - avatar