Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
¿Por qué useReducer no actualiza el estado en el contexto de React?

Estoy usando useReducer y context para actualizar mi estado en App.js y luego enviar datos a la base de datos, pero no actualiza el estado y siempre es nulo.

Aplicación.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>

Contexto de cita en bloque

Creo un componente de contexto separado

contexto.js

 import React from 'react'; export const AuthContext = React.createContext();

Pantalla de cita en bloqueA

En screenA, uso el correo electrónico e inicio de sesión para enviar datos a App.js y luego guardo estos datos en la base de datos 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 answers
Answer question

0

El estado se está actualizando, el problema es que está tratando de escribir el valor en la consola justo después del "envío", en ese momento el estado aún no se actualizó. Si desea ver el valor actualizado, agregue este useEffect a App.js

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

Este useEffect se activará cuando cambie "loginState.email".

Debería considerar usar React Developer Tools para validar los cambios de estado.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!