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
1
vote
1 answer

React Typescript errors on useReducer login

I am doing a proyect on React Native and Typescript, I am new on them and I had been having an issue lately that I couldn't solve yet. I am trying to do a login on the app and Typescript keeps giving me errors on a useReducer I did. Unfortunately, i…
1
vote
1 answer

When declare useContext, Error: Invalid hook call. Hooks can only be called inside of the body of a function component

I'm creating an AuthContext that will be used on the LoginPage, there is no error in VSCode but when I run the application it gives an error like the following: Error: Invalid hook call. Hooks can only be called inside of the body of a function…
1
vote
1 answer

present : past[past.length - 1] , this should return a single value but it is returning a full array like slice method does

import React, { useState, useRef, useReducer } from "react"; import "./App.css"; function reducer(state,action){ const {past,present,future} = state switch(action.type){ case 'ADD' : return { past : [...past,present], present…
Adan Ali
  • 9
  • 5
1
vote
0 answers

Fetching data asynchronously with React's useReducer()

This is in continuation to the following answer: https://stackoverflow.com/a/61923286/9042295 For method 1: Fetch data before dispatch (simple), dispatch is asynchronous in my knowledge. If that's the case, is it possible to make sure that is…
Aditya Verma
  • 177
  • 8
1
vote
1 answer

Why functional component does not rerender after change of the state in useReducer()

I use react Hooks, especially useReducer. State changes, but functional component does not rerender. The drawer should be opened after pressing the button in the menu. State changes but drawer does not open. This is sandbox witj code…
Festina
  • 327
  • 3
  • 17
1
vote
1 answer

How to setup a generic interface for the state when using React useReducer with typescript

I face this problem when I try to setup an interface to the state when trying to use useReducer.I use it in a custom hook for fetching data from the server. The data object in response from the server may contain different properties. the…
1
vote
0 answers

react useReducer hook with user object

I am experimenting with the useReducer hook in React here is my following code in sandbox: https://codesandbox.io/s/usedispatch-users-c58t4?file=/src/App.js I create a user object with an empty array called interests. The array is initially empty…
Ben Shekhtman
  • 385
  • 5
  • 15
1
vote
1 answer

how can i use useReducer react js with firestore? it always return undifined

how can i use useReducer react js with firestore? it always return undifined I'm trying to use reducer with firebase, but if I use dispatch in useEffect, I can't get the array. The userType works fine. What did I do wrong? When the app.js page is…
KH K
  • 13
  • 2
1
vote
1 answer

React useContext & useReducer - Need global state in app, confused about using one vs multiple reducers

I need to share pieces of information across my app, like user details, avatar, and in-app alerts. I'm concerned I might not be using useReducer and useContext the right way since I'm just subscribing everything to this one reducer and using it as a…
Bryson Kruk
  • 281
  • 4
  • 12
1
vote
1 answer

When I click to increase the quantity of an item in cart, It increases two instead of one

I am facing an issue with the shopping cart. After clicking on INCREASE_QTY to increase the quantity of an existing item in the cart, It increases two instead of one. Total item quantity increase correctly but single item quantity now increases…
Adil Rao
  • 19
  • 1
1
vote
1 answer

React: useReducer Hook trigger twice at first, NOT StrictMode

this is NOT the problem cause by StrictMode. here is my code first, a Counter: let outsideCount = 0 function Counter() { const [count, setCount] = useReducer(doCounting, 0) function doCounting(count) { console.log(`count: ${count}`) …
FlyC
  • 1,844
  • 1
  • 15
  • 18
1
vote
1 answer

why I am getting never type in useReducer in TS?

I am making a reducer state of three values, conversations- intially empty array with the described properties, messages - intially empty array with described properties and activeConversation string. But I keep on getting typescript never errors. I…
Gurkiran Singh
  • 189
  • 2
  • 11
1
vote
2 answers

useReducer() is returning data for one component and for other component it is returning the initialState

I actually have 2 components that require data from a reducer that has some state there. The CurrentWeather component shows the weather at a very high level and the CurrentWeatherDetailed in a detailed way. The first one is rendered first, and if…
1
vote
1 answer

Can't Access React Reducer Value from Function

In the code below, a keypress event detector is implemented through useEffect which attempts to update index through the useReducer function. However, the console.log output doesn't seem to show that the update works even though the button…
An0n1m1ty
  • 448
  • 4
  • 17
1
vote
1 answer

Why is useReducer hook not updating state?

I am working on login/register components in React, and I'm using useContext and useReducer hooks to manage state. This is the first time I've tried it this way, and I'm not sure why the state is not changing. Below are the files for the login…
maria05
  • 13
  • 1
  • 5