Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
Why useReducer doesn't update state in React context

I'm using useReducer and context to update my state in App.js then send data to the database but it doesn't update the state and always is null.

App.js

import AuthContext from './context';
import screenA from './screenA';

export default function App() {

 const initialLoginState = {
   email: null,
 };
const loginReducer = (prevState, action) => {
   switch (action.type) {
     case 'Email':
       return {
         ...prevState,
         email: action.Email,
       };
   }
 };

const authContext = React.useMemo(
   () => ({
     email: emailUser => {
       dispatch({type: 'Email', Email: emailUser});
       console.log(loginState.email);
     },
     signIn: async () => {
       try {
         fetch('FakeApi', {
           method: 'POST',
           headers: {
             Accept: 'application/json',
             'Content-Type': 'application/json',
           },
           body: JSON.stringify({
             email: loginState.email,
             date: '2021-9-20',
           }),
         });
       } catch (e) {
         console.log(e);
       }
     },
   }),
   [],
 );
 const [loginState, dispatch] = React.useReducer(
   loginReducer,
   initialLoginState,
 );

return (
   <AuthContext.Provider value={authContext}>
         <screenA />
   </AuthContext.Provider>

Blockquote context

I create a separate context component

context.js

import React from 'react';

export const AuthContext = React.createContext();

Blockquote screenA

In screenA I use email and signIn to send data to App.js and then save this data in the database screenA.js

import {AuthContext} from './context';

function screenA() {
 const {email,signIn} = React.useContext(AuthContext);
 return (
   <View style={{marginTop: 150}}>
     {/* Count: {state.count} */}
     <Button
       title="sent email"
       onPress={() => {
         email('example@gmail.com');
       }}
     />
     <Button
       title="signIn"
       onPress={() => {
         signIn();
       }}
     />
   </View>
 );
}

export default screenA;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The state is being updated, the problem is you are trying to write the value to the console right after "dispatch", at that point in time the state wasn't updated yet. If you want to view the updated value, add this useEffect to App.js

  useEffect(() => {
    console.log(loginState.email);
  }, [loginState.email]);

This useEffect will be triggered when "loginState.email" change.

You should consider using React Developer Tools to validate state changes.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda