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
Dispatch (useReducer) function not working when passing it as props (React)
When user clicks a button, I want to show a component for 2s.
After the 2s, I want to run a function(s) that are coming from props. But, the dispatch function is not working.
Any idea how can I fix it?
const Expire = (props: any) => {
const…

Alfrex92
- 6,278
- 9
- 31
- 51
0
votes
0 answers
What's the difference between current state and initial state in useReducer function?
I'm new to Web development and react, I'm trying to learn useReducer and I don't quite understand the difference between currentstate and initial state.
const [state,dispatch]= useReducer(reducer, state).
On reading blogs on useReducer I…

Krishna Bharadwaj
- 17
- 7
0
votes
0 answers
Getting undefined while using Usereducer in react
I am getting the below error while using UseReducer hook in react! I've destructured the state object however when I am using it I am getting this error!:-
const [{previousOperand,currentOperand,operation},dispatch] = useReducer(reducer,{});
…

Prabadhya Upadhyay
- 11
- 1
0
votes
0 answers
asyncstorage with useReducer does not save the values
i am new to reactnative, i was trying to build todo app using useReducer and asyncstorage but not able to achieve that, i have read every article and related questions i am able to do it using useState but did not got the result with…

Shivam Saini
- 23
- 7
0
votes
1 answer
Can't use useReducer Hook with in a Typescript Next Js project
I am very new to Typescript and I am here trying to use the useReducer hook with Typescript.
This is my code:
import { useReducer, useContext, createContext } from "react"
import type { ReactNode } from "react"
const defaultState = { count:…

Gourav Kumar
- 77
- 6
0
votes
0 answers
Combine reducer when create state management use hooks useReducer and useContext in ReactJs
I'm learning ReactJs. I'm creating state management by useReducer and useContext through some instructions. I have a problem when combining reducer and state. When I dispatch data, I can't get the initial state in the reducer. Below is my code for…

Văn Hiệp Nguyễn
- 13
- 1
- 3
0
votes
1 answer
React: How to clear input value with useReducer() hook after form submit
I have an input where the user should have the last name, I'm using the useReducer() hook for validation if the input value includes a space (' ') and storing the value. Everything is working fine I'm also getting an invalid message correctly.
But…

Mohit
- 135
- 10
0
votes
1 answer
How to properly change the button disabled state in a useReducer based off of password fields matching?
I am working with a basic form. The form contains a username field, email field, password field, and a repeated password field. I'm trying to solve state management by throwing the form state in a useReducer. However, I'm not getting the…

Bryant Wooters
- 9
- 1
0
votes
1 answer
TypeScript: Problem typing a custom Form hook
I know this is gonna be messy and terrible but while I have the TypeScript basics down, I'm having trouble typing the more advanced concepts, particularly typing useReducer hooks.
Here's my code with what I have so far, but I understand I have…

HarrySIV
- 115
- 9
0
votes
1 answer
How to call a Function which is called inside Reducer, and the said Function uses the State returned by useReducer?
React v17
I am using useReducer. In the reducer function, I need to call another function check for network fetch. In check( ), I need to access the state, which is returned by useReducer.
I am getting this error can't access lexical declaration…

Arindam
- 163
- 1
- 13
0
votes
2 answers
refresh user data when changes are made to mongodb
I am new to react and MongoDB, I have this site where the user can log in and look at his detail like name…, coming from the db. I am trying to find a way to update what is shown to the client when a change is made directly from the database, or…

Mega Animenia
- 15
- 1
- 6
0
votes
1 answer
Using setState from inside a reducer. Reducer updates state twice instead of once
I am new to React and I'm trying to build a calculator in React.js. I have a display that contains two fields: one to output the current value and the other one to store a "slice" of the expression. I'm basically trying to immitate the Windows…

superpav
- 47
- 1
- 5
0
votes
0 answers
++ Incrementing by 2 instead of 1 in React JS
The code below is supposed to increment & decrement the quantity by 1 but instead, it is incrementing and decrementing by 2
switch(action.payload) {
case "INC_QUANTITY": {
let newState = state;
newState[action.payload].quantity +=…

Napstar
- 77
- 1
- 10
0
votes
2 answers
useRecuder + typescript error Argument of type '(type, action) => { state: (...}' is not assignable to parameter of type 'ReducerWithoutAction'
Im trying to learn typescript so i decided to do a simple shopping cart app
GitHub repository: https://github.com/CsarGomez/shopping-cart-reducers-tx
im getting the following error:
No overload matches this call.
Overload 1 of 5, '(reducer:…

Cesar Gomez
- 37
- 1
- 7
0
votes
2 answers
Updater function with useReducer?
In react, with useState, I can pass it an updater function to get the current state within the same function. Currently I update a state multiple times within the same function.
Is that possible with a reducer dispatch with useReducer?
Thanks.
const…

hmpws
- 23
- 1
- 3