Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,879 members, 7,817,590 topics. Date: Saturday, 04 May 2024 at 03:07 PM

Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) (1332 Views)

Redux RTK Vs React Query For Fetching Data And Inifiite Scroll / Guys, I Need Help With This Reactjs Issue Please. / If You Are Good With React Hooks And Context API (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 3:09pm On Jan 18, 2023
Guys I am creating this ecommerce store app using context api and useReducer for state management. I am trying to increase the quantity of each item using the "amount" variable, but for some reason it is not working. I have examined and re-examined the logic in the reducer function and it seems okay, but yet when i click on the button in my shop page, nothing happens. It doesn't event return an error. I'm really confused. Can someone look through the code and tell me where the bug is?? Thanks.

edit: I refractored the code using redux tool kit and I am still having the same issue. Omo!

Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 3:21pm On Jan 18, 2023
Please someone should help me sort this out, I am having headache already, and I don't tend to stop until I've figured shit out.

1 Like

Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 4:45pm On Jan 18, 2023
So no one can help me? I've seen reactjs, truthcoder and qtguru view this thread.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 4:49pm On Jan 18, 2023
looking..
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 5:03pm On Jan 18, 2023
Why not use the debugger to enter the call stack and see if it hits the reducer, it's hard to debug at face value, is it online ?
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 5:03pm On Jan 18, 2023
debug the addOne method, if you can jump on Google meet or hit us on our slack.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 5:14pm On Jan 18, 2023
@devdevdev
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Karleb(m): 8:37pm On Jan 18, 2023
Devdevdev:
Guys I am creating this ecommerce store app using context api and useReducer for state management. I am trying to increase the quantity of each item using the "amount" variable, but for some reason it is not working. I have examined and re-examined the logic in the reducer function and it seems okay, but yet when i click on the button in my shop page, nothing happens. It doesn't event return an error. I'm really confused. Can someone look through the code and tell me where the bug is?? Thanks.

Have you tried to return the eachCart variable?
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 8:42pm On Jan 18, 2023
qtguru:
Why not use the debugger to enter the call stack and see if it hits the reducer, it's hard to debug at face value, is it online ?

I switched to redux tool kit and I am still having the same issue. The clearCart and removeItem functionality are working just fine. It's just the addOne that isn't. I think the issue is with the 'amount' variable and how it's state is getting altered. I am seriously confused.

Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 8:47pm On Jan 18, 2023
Karleb:


Have you tried to return the eachCart variable?
I already did within the reducer function
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 8:51pm On Jan 18, 2023
In the initial state, the amount has the type of number, but when I do the "cartItem.amount" , the amount changes to the type "any" and hence can not compute as it isn't a type number

Someone should help me figure this out, I am seriously about to start crying.

Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 8:53pm On Jan 18, 2023
GREATIGBOMAN, help me solve this issue and you will have my respect, and I will officially apologize for all the insults. I might even get you a gift. grin
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Nobody: 8:57pm On Jan 18, 2023
Devdevdev:
GREATIGBOMAN, help me solve this issue and you will have my respect, and I will officially apologize for all the insults. I might even get you a gift. grin

I don't need any apologies.

If u are committing the code somewhere just send the link let me clone it.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 9:00pm On Jan 18, 2023
GREATIGBOMAN:


I don't need any apologies.

If u are committing the code somewhere just send the link let me clone it.

I am not committing the code anywhere, for now. It's a pretty straight forward code, especially with the redux tool kit. Just replicate the functionality, please.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:01pm On Jan 18, 2023
Devdevdev:


I switched to redux tool kit and I am still having the same issue. The clearCart and removeItem functionality are working just fine. It's just the addOne that isn't. I think the issue is with the 'amount' variable and how it's state is getting altered. I am seriously confused.

want to jump on google meeting, it's hard to debug on image
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:01pm On Jan 18, 2023
also the item.id === payload what is that logic ?
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Karleb(m): 9:05pm On Jan 18, 2023
Try to log out cartItem and the payload too. That might give you a clue.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Nobody: 9:08pm On Jan 18, 2023
Devdevdev:
In the initial state, the amount has the type of number, but when I do the "cartItem.amount" , the amount changes to the type "any" and hence can not compute as it isn't a type number

Someone should help me figure this out, I am seriously about to start crying.

Try wrapping this in Number(cartItem.amount)
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:10pm On Jan 18, 2023
This is how the Dispatch is, it requires a type, also the point of the reducer is to return a new object combined from previous state and new update. What you're doing is an impure function. Look at this code i uploaded.

Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 9:11pm On Jan 18, 2023
qtguru:
also the item.id === payload what is that logic ?

I am looking for an item from the cart that the id matches the action.payload. The payload is the information that is dispatched to the button that is clicked when i import the useDispatch( ) function. In this case, the product.id from my data is the payload. The action is the command passed.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:12pm On Jan 18, 2023
When you call a Dispatcher, it uses the type to know which reducer to call, it expects a return value, but you are doing a side-effect instead of returning something like this e.g
{...state,newProps}
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 9:13pm On Jan 18, 2023
GREATIGBOMAN:


Try wrapping this in Number(cartItem.amount)

I did, got a big, fat error.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:14pm On Jan 18, 2023
The value return becomes the new state, and triggers every component listening to that, allowing them to receive the new copy. Try my suggestion.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:15pm On Jan 18, 2023
Do this in your reducer. e.g return {...state, {amount: action.payload | value }} something like that
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 9:16pm On Jan 18, 2023
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:18pm On Jan 18, 2023
Devdevdev:

You are using useDispatch wrong, because your code won't know which reducer to call I think

Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:24pm On Jan 18, 2023
Get rid of the Context, you're already using Redux Hooks, you don't need it. Just use only Redux, let each component query the reducer it needs.
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Nobody: 9:30pm On Jan 18, 2023
Devdevdev:
Guys I am creating this ecommerce store app using context api and useReducer for state management. I am trying to increase the quantity of each item using the "amount" variable, but for some reason it is not working. I have examined and re-examined the logic in the reducer function and it seems okay, but yet when i click on the button in my shop page, nothing happens. It doesn't event return an error. I'm really confused. Can someone look through the code and tell me where the bug is?? Thanks.

edit: I refractored the code using redux tool kit and I am still having the same issue. Omo!


const reducer = (state, action) => {
switch (action.type) {
case "INCREMENT":
let eachCart;
eachCart = state.products.map((product) => {
if (product.id === action.payload) {
return { ...product, amount: product.amount + 1 };
}
return { ...state, products: eachCart };
});
default:
return state;
}
};

replace your reducer with this and try:

const reducer = (state, action) => {
switch (action.type) {
case "INCREMENT":
let eachCart = [...state.products];
let index = eachCart.findIndex(
(product) => product.id === action.payload
);
eachCart[index] = {
...eachCart[index],
amount: eachCart[index].amount + 1,
};
return { ...state, products: eachCart };
default:
return state;
}
};
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by Devdevdev(f): 9:31pm On Jan 18, 2023
qtguru:
Get rid of the Context, you're already using Redux Hooks, you don't need it. Just use only Redux, let each component query the reducer it needs.

I already did. Still same issue.

Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:33pm On Jan 18, 2023
Devdevdev:


I already did. Still same issue.

1. You didn't refactor the reducer to return a new state
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:33pm On Jan 18, 2023
2. your dispatch is meant to take an object , you also didn't change that too
Re: Please I Need Help With This Reactjs Issue - Context Api & RTK (pictures) by qtguru(m): 9:34pm On Jan 18, 2023
Your code is wrong. Fix those issues I pointed out, A reducer should always return a new state always

(1) (2) (3) (Reply)

Create A Bot For M$n And Win $ / What Programming Language Did You Learn First? / Best Core Java Tutorial That I Want To Share

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 36
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.