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
1 answer
I'm not sure how to access and compare an object when keys are made using Date.now()
I'm quite new to coding and I'm currently practicing the useReducer() hook in React to manage some state in a simple todo app.
I'm having trouble when trying to implement the TOGGLE_TODO action. I've done it before using arrays, but as I'll likely…

MagicMark82
- 13
- 2
0
votes
2 answers
I can't figure out how to edit array items using a map function in React
So I have been trying for days to solve the following issue:
movies [
{
id: 1,
name: 'Alpha',
rating: 0
},
{
id: 2,
name: 'Bravo',
rating: 0
},
{
id: 3,
name: 'Charlie',
rating: 0
}
]
I already have a…

TeflonTrout
- 13
- 4
0
votes
1 answer
In react useReducer store loses state when I route to another page. useContext loads only initial state
State is saved well on page and gets changed and interactive, but once I navigate to another page - state is reset
The whole store and createContext is
type Action2 =
{
type: "ON";
payload: boolean;
}
| {
type: "OFF";
…

3cirlces
- 105
- 11
0
votes
2 answers
Is a React Context basically a mini redux store?
No practical purpose to this question, just tryna connect concepts to get my head around switching from Redux to using useReducer and React contexts - is a Context when used with useReducer` basically a redux store that's only available to…

Ollie Khakwani
- 754
- 1
- 8
- 17
0
votes
1 answer
Writing to firebase from dispatch results in duplicate rows
I am playing with a simple app that displays a collection of Posts from firebase and allows the user to add docs to it.
The posts are shared via a PostsContext that contains an ADD function. The useReducer gets called twice, no getting around that.…

kozureookami
- 151
- 2
- 8
0
votes
1 answer
How to obtain state of a React Component to implement a global Save-Button
We develop a reusable table-like component with Reacts functional component style.
The table holds, manages and validates it's state with React.useReducer.
In each row there are several input-fields, checkboxes and action-buttons to modifiy the…

Ogod
- 878
- 1
- 7
- 15
0
votes
0 answers
useReducer not updating the array of items in sync, being a render late
A video with the behavior, I am clicking one time in 'A' and one time 'D', alternatively and is like there is two states, really strange!
https://www.loom.com/share/ba7a97f008b14529b15dca5396174c8c
And here is the action to update the…

Vyse Clown
- 49
- 1
- 8
0
votes
1 answer
How to prevent infinite loop with React's useReducer, useContext and useEffect
I'm currently trying to figure out how to avoid creating an infinite loop when wrapping my application in a Context provider (taking in values from useReducer) and then updating via child component with a useEffect hook.
There's an example of the…

thathurtabit
- 558
- 2
- 11
- 22
0
votes
1 answer
ReactJs : useReducer on array of object. I need more understanding
Still new to the world of Js and React, I just made a website that works. But it bothers me the way I made it work. And I would like a bit of insight into how I could do things the better way.
So here I made a simple project on the sandbox to…

Othman
- 72
- 10
0
votes
1 answer
TypeError: Cannot read property 'map' of undefined - React app - useContext and useReducer issue
I am using react for making an app project using contextHook and reducerHook but getting:
TypeError: Cannot read property 'map' of undefined
The same code was working a few days back, but now it's giving that error. I've wasted so many hours…

Muhammad Daniyal
- 11
- 5
0
votes
1 answer
React Native Context Content is keep Coming Back
I want to clear user and do logout in React Native using useContext, useReducer, and useMemo.
My App (AuthContext) has Users Screen (UserContext) in it.
UserContext has clearUsers() within useMemo
AuthContext has logout() within…

Jeaf Gilbert
- 11,495
- 19
- 78
- 105
0
votes
2 answers
Is it safe to throw an Error in a React Reducer?
As discussed here, it seems that React reducers must be pure functions and never generate side-effects (no API calls changing anything). Is it safe to throw an Error in a reducer in case of an invalid input)? (Isn't that considered a side-effect?)

Shayan
- 2,758
- 7
- 36
- 55
0
votes
1 answer
How to stop re-rendering when using useContext (Reactjs)?
I have a simple react app in which there is a FruitsList component for showing the fruits in the list, a FruitForm component to add a fruit, and both are contained inside a Fruits component. I am using useContext and useReducer to manage the state…

Uddeshya Kumar
- 41
- 5
0
votes
1 answer
How to make boolean state setter reusable
I currently have a page with headers: h1, h2, h3. I want their text to be editable by a user onClick, so basically the header will turn into input type='text' then revert back to the header when user is done entering his values in the input. I have…

Jim41Mavs
- 482
- 4
- 21
0
votes
1 answer
Reducer payload of array with new order does not update the state
First of all, here is a CodeSandbox: https://codesandbox.io/s/todolist-reducer-0y3mb?file=/src/context/TodosReducer.js
The problem is that the state is not updating even though I'm passing a payload with a new array to the reducer.
I'm rearranging…

Mattias Bregnballe
- 101
- 6