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
2 answers
Content is not loaded every time on reloading screen using Reactjs and React UseReducer hook
export default function TableBooks(props) {
const reducer = (state, action) => {
switch (action.type){
case "start":
return { ...state, loading: true }
case "loader":
initial.after = initial.after+ action.newData.length
return {…

Pratyush Narain
- 381
- 2
- 9
0
votes
2 answers
React - problem using useContext & useReducer
Hey I had a problem while using useContext (React Hooks) and useReducer (for store a array with 4 values of string). Explained in the pictures down below what exactly it's wrong and what I have done. Thank u
App.js - using ItemsByCategory (Line 7).…

Hadar Zaguri
- 11
- 2
0
votes
1 answer
React Hooks: useEffect, useReducer, createContext and localStorage; TypeError: Cannot read property of undefined
I am using local storage in my Map app for some persistent storage; also using React.createContext coupled with useReducer to share and update state amongst components.
State in local storage, and in the app when consoled are updating and present,…

Antonio Pavicevac-Ortiz
- 7,239
- 17
- 68
- 141
0
votes
1 answer
TypeScript — How to infer key-value type based on dynamic key
Suppose I had a state object with an interface such as this:
interface IState {
car_brand: string;
year: number;
+ many other properties with various types
}
and I wanted to produce a reducer function for changing values in this state.…

Colin Turner
- 31
- 2
0
votes
2 answers
useReducer: Not able to get the initial state using userReducer
I have implemented useReducer to dispatch the action based on the action type and each time I'm updating a state variable based on the action type but always I'm getting an error while trying to read the initial state. Probably my code can explain…

vivek.p.n manu
- 286
- 1
- 4
- 19
0
votes
1 answer
Data get updated twice on button onClick event
I've a very simple React Web App where I want to add new object of review to an array of an object:-
I'm using useReducer to handle default state of my data as show below:-
reducer function:-
const reducer = (state, action) => {
switch…

lala
- 1,309
- 9
- 21
0
votes
0 answers
Callback using old state when called
I have a modal where I pass in the onConfirm callback that triggers when user clicks the confirm button. The issue I'm seeing is that the state in this function is not the current state but the initial application state.
app.js
const discardClaim =…

Batman
- 5,563
- 18
- 79
- 155
0
votes
1 answer
UseReducer is called twice when dispatching data from useEffect hook within child component
I am making a todo/shopping list in ReactJS. Besides being able to add items manually to the list by input, the user should also be able to add items programmatically.
I am using createContext() and useReducer for managing the state().
When I add…

Ash 8851
- 3
- 4
0
votes
0 answers
call dispatch function from another useReducer
I'm kind a new to using userReducer with Context API, so I'm not sure how I can call dispatch function from another useReducer.
useReducerB
...code...
function someFunction()
{
dispatch(otherActions.B) // this action i like to call from another…

Игор Ташевски
- 474
- 1
- 6
- 13
0
votes
1 answer
React Store not updating context with React.useReducer
I'm building a simple chat room with React and would like to be able to update the chat window with text by typing in the text field and pressing the send button.
I was expecting that when the Store that holds the context invokes React.UseReducer,…

yupyupyup
- 63
- 8
0
votes
1 answer
useState is not properly updating after selections inside dropdowns
I have a function to disable a dropdown until selections have been made in 2 other dropdowns. It is not enabling the dropdown properly I believe due to some asynchronous issues with useState.
const [homeSelect, setHomeSelect] =…

Zero Cool
- 2,541
- 2
- 8
- 13
0
votes
0 answers
dispatch is not updating reducer
I'm using useReducer and useContext as a flux store and i'm boggled on why i'm unable to update the stores state. Here is a sample of the code I'm working with.
reducer and initial state
export const localState = {
modal_open: false,
date:…

rtwhite
- 399
- 1
- 3
- 8
0
votes
1 answer
Is there a way i can get access of a component's state to another component through useReducer and Context API?
I found this code in a youtube video in which a counter in App.js is global and is being controlled by button in the app component as well as by buttons in other components by passing a function on click event of button that dispatches an…

Rija Shaheed
- 69
- 2
- 7
0
votes
1 answer
What's the work around to use reducer dispatch to work outside React component without using store
I have setup a reducer in a project which uses react, typescript and nextJS.
Is there a way I can use this reducer to dispatch the event outside react component i.e (APIservice).
I have found answers which instructs to use store, but in my case I…

sinthu225
- 301
- 3
- 16
0
votes
0 answers
useReducer in useContext not saving state
I am trying to fetch data from my database and then using useReducer if the fetch is successful I then want to return it as state so I can pass it down to components.
I have console.log'd req.data and it is fetching the correct data however it isn't…

Mitchell Cartwright
- 181
- 3
- 11