For questions regarding the `useReducer` hook in React. `useReducer` is an alternative way to manage complex state by defining a reducer function that creates the next state based on the current state and a dispatched action.
Questions tagged [use-reducer]
495 questions
0
votes
0 answers
useReducer() hook with object: Why does context not update even though the object is updated?
I have been using useReducer() to update a counter for a webapp.
Here's an example of what my code looked like:
type FruitCollection = {
apples: number;
};
const reducer = (
countState: FruitCollection,
action: FruitCountReducerActions,
) =>…

Patrick Kenny
- 4,515
- 7
- 47
- 76
0
votes
2 answers
React Context states getting erased automatically and unable to update individual fields
I have a simple reducer/context system to store states. Everything is working fine except that I cannot update fields individually without affect the state of the others.
The problem is that, when I change one of the AppData fields, the other fields…

Irina
- 59
- 7
0
votes
2 answers
I believe I have created a shareable state using useReducer only, I need advice if this is wrong
Store.js:
import {useReducer} from "react";
import {ACTION_TYPES} from "./storeActionTypes";
export const INITIAL_STATE = {
counter: 0,
};
export const storeReducer = (state, action) => {
switch (action.type) {
case…

MarioWeb
- 3
- 1
0
votes
1 answer
Learning react, trying to understand useReducer()
I just learned about the useReducer hook and wanted to try to experiment some to see if I could get a better handle on how it works.
I have a very simple app that when a button is clicked, the background changes to the color of the button's label,…

Kade
- 5
- 3
0
votes
1 answer
State not updating with use reducer
I've been trying to update state when fetching data from an API using the use reducer hook, but it's not updating even though my API request was successful, and I can see the data. Is there something I'm doing wrong? I've checked my console, and it…

Abby
- 1
0
votes
0 answers
How can I determine when a useEffect should be rendered? is it posible to determine in which moment it can be mounted in the component in React?
I've been trying to find out how to set when a function should be triggered within the useEffect() hook and therefore rendered.
I need to render on a table the transactions made from the user logged in in the app by passing the user id, which is the…

Rosh343
- 33
- 6
0
votes
0 answers
Doing CRUD using useReducer Hook in ReactJs update tod doesnot work
--> context/TodoContext.js // the main problem is that suppose i added a todo and when i click on edit then all todos disappers like when i refresh the page but the todo gets updadted and only that todo(that i updated) appears on todo list.so i need…

Bipin Bhandari
- 1
- 2
0
votes
1 answer
Use state after dispatch finishes
In my code, upon submission of a form, I need to modify the state & then submit the updated state. Since useReducer is async, I can't access the updated state before form submission. How do I resolve this issue ?
const [data, dispatch] = …

Narayan Waraich
- 69
- 1
- 6
0
votes
2 answers
React filters products in a one go
I just came from the interview, I have implemented multiple filter feature in my assignment.
assignment is live here: https://tooth-store.netlify.app/
here is my code where I am filtering with according to the value of filters state.
const…

Mohammad Farhan
- 131
- 1
- 8
0
votes
0 answers
How to infer K property name from a generic T object type inside a React reducer?
So im trying to create my own custom hook to handle all my forms.
For that im using the useReducer hook to handle al input changes and save them in the state.
The handleChange method recives two arguments (inputName, inputValue) and should update…

Nahuel M.
- 146
- 8
0
votes
0 answers
react useReducer() best practice
I'm currently working on a notification component using toast-react and useReducer hook. I'm thinking to trigger the notification in the reducer method. Will there an issue/side effects with this approach?
First time to use react's userReducer()…

Wreeecks
- 2,186
- 1
- 33
- 53
0
votes
0 answers
Calling dispatch function of useReducer (used in useContext ) causes the subscriber components to re-evaluate
The problem is that when I call the dispatch function of the useReducer hook declared in the Context SelectedMealContext , the gets re-evaluated even if I am not changing the state of useReducer in the context.
My component has…

Muhammad Mehdi
- 531
- 1
- 5
- 14
0
votes
1 answer
List Items on react component not updating using useReducer() when clicking delete button
I am using use reducer inside a ReactContext to handle some signatures display. On Messages and messagesForm components the dispatch function works fine. But inside prevMessages it doesn't.
I tried console log (json) but it is also not working…

E.K.Garcia
- 49
- 2
- 7
0
votes
1 answer
How to return initial state with useReducer?
I'm using useReducer with useContext to manage state, but I can't return initial value of state in a child component.
In my reducer file I have:
import context from './context';
const initialValue = {
case1token: null,
case2token:…

jdez
- 189
- 1
- 10
0
votes
2 answers
How to persist counter variables using useReducer in React
I have two objects that I'm trying to loop through using an increment and decrement function. I have the variables being stored in localStorage and the expectation is that no matter which value has been selected last, upon refresh or reload, the…

paq
- 87
- 8