Questions tagged [use-reducer]

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.

495 questions
0
votes
1 answer

switch case in useReducer is being called incorrectly

I am creating an app and using useContext+useReducer to manage my state. There is a functionality where I let user create a project and then append this project to projects array state and navigate user to the next screen where he can add more…
ms394
  • 109
  • 7
0
votes
1 answer

useReducer - Enforce only one true value without explicitly setting others false?

I'm looking for a smarter way to enforce one value true, setting the others to false This Python q&a suggests an enum (I'm not familiar with either) I can already switch (action.type) { case 'loadCurrent': return {...state,…
Zach_is_my_name
  • 59
  • 3
  • 14
0
votes
1 answer

React useReducer conditional dispatch

I am trying to implement a "use company address" button that autofills the inputs if selected. I get the pre-filled values as expected, however when I hit submit, the data for these fields are blank/didn't update. My Code: …
adcanis
  • 1
  • 3
0
votes
1 answer

React useReducer() fired twice , always add double quantity to shopping cart?

In strict Mode, React runs twice to detect side effects. So I think I should not turn the strict Mode off, I should make sure that when running twice, the output still correct. There is a product , when I add qty 3, run twice , the output will be 1…
Miki U
  • 21
  • 6
0
votes
0 answers

How do update nested object field with useReducer?

I am new to using useReducer, I need to write a reducer function for this. These will be hooked into text fields and I am trying to update the state this way. const [registerReq, updateRegister] = useReducer(registerReducer, { user: { …
0
votes
1 answer

use Reducer function with ternary condition

I am learning use Reduce function. I have text (1 sentence) with btn. When I click on btn. it shows me full text (5 sentences) + modal show up, when I click btn again text return to one sentence. It is toggle function. All is working. I want to add…
pete
  • 45
  • 5
0
votes
0 answers

BasicForm.js:7 Uncaught TypeError: Cannot read properties of undefined (reading 'trim')in react while using useReducer,please help to solve this error

I a'm getting error BasicForm.js:7 Uncaught TypeError: Cannot read properties of undefined (reading 'trim') at BasicForm.js:7:1 My Code //---my use-input-hook--------- import { useReducer } from "react"; const intialInputState=()=>{ …
0
votes
1 answer

Problem with increasing product quantity in the cart using usereducer (React)

I have a problem increasing and decreasing the number of products in the cart of only the targeted product using the use-reducer in React. When ever i click on the increase or decrease button, i recieve error and the entire display disappear Here is…
Rengkat
  • 23
  • 8
0
votes
1 answer

Removing the last item of an array with filter won't re-render react component

Inside a reducer I'm using the filter method to delete a movie from a localStorage on user click. All the movies are deleted from the localStorage except the last one that will be delete from my state, but not from the localStorage. It seems the…
ekno
  • 69
  • 6
0
votes
1 answer

Unexpected behaviour of useReducer hook in React JS

Can anyone just let me know why I am getting two console prints here? Here below is my code: import React, {useReducer} from 'react'; const Counter = () => { const initialState = { count: 0 } const reducer = (state, action) => { …
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
0
votes
1 answer

Export actions when we use the UseReducer hook approach for React app

I have an application built by React hooks. So, In the case where using userReducer I have special actions file like this export namespace PrepareReviewActions { export enum Types { TOGGLE_CONFIRMATION, TOGGLE_ALL_CHECKED, …
pridan
  • 163
  • 1
  • 6
0
votes
0 answers

Why does React useReducer lose intermediary state updates with multiple actions?

Consider the following setup: const [state, dispatch] = useReducer((_state, action) => _state + action, 0); return
{state}
With the above code, if I fire the dispatcher with dispatch(2), the render will reflect '2' and if I later fire…
user2765977
  • 491
  • 4
  • 18
0
votes
1 answer

React reducer - data.map is not a function or how to add values to case in a reducer

I have this reducer: export const reducer = ( state, { type, payload } ) => { switch ( type ) { case 'SET_TOURS_IN_CART': return { ...state, toursInCart: payload }; default: return state; } }; I…
DavSev
  • 1,005
  • 4
  • 22
  • 47
0
votes
1 answer

Best way to manage my application state, useState vs useReducer?

I've been using React for some time but know I want to improve my knowledge to another level so I want to know what would be your approach for managing the state on this kind of application? import { Pedalboard } from…
Ashel
  • 21
  • 2
0
votes
1 answer

useContext does not forward dispatch function from useReducer

I am trying not to use Redux. So I am stuck with useContext in combination with useReducer for globaal state management. My problem: I can not update the state from a child component using dispatch. Let me explain in more detail. My context file is…
Twos22
  • 57
  • 1
  • 11