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

187
Visualizações
After send value with mapDispatchToProps, redux Initial state is undefine

Hello I am making login app used firebase google login. I want to save the user in the user-reducer. The console shows user info from the firebase! but, It keeps shows undefined. I don't know why it shows like these. Please advised!

enter image description here

[user-action ]
import { UserType } from './user.types';
export const login = ({ currentUser }) => ({
  type: UserType.USER_LOGIN,
  payload: currentUser,
});

[user-types ]

export const UserType = {
  USER_LOGIN: 'USER_LOGIN',
};

[user-reducer]

import { UserType } from './user.types';
const INITAIL_STATE = {
  user: [],
};
const userReducer = (state = INITAIL_STATE, action) => {
  switch (action.type) {
    case UserType.USER_LOGIN:
      return {
        ...state,
        user: action.payload,
      };
    default:
      return state;
  }
};

export default userReducer;


[Login.js]


const Login = ({ googleLogin }) => {
const [currentUser, setCurrentUser] = useState(null);

  const getUser = new Promise((resolve, reject) => {
    console.log('doing...');
    firebase.auth().onAuthStateChanged(setCurrentUser);
    resolve(currentUser);
    console.log(currentUser);
  });

  getUser
    .then(user => {
      googleLogin(user);
    })
    .catch(error => {
      console.log(error);
    });

 .... 

const mapDispatchToProps = dispatch => ({
  onModal: () => dispatch(modalHandler()),
  googleLogin: currentUser => dispatch(login(currentUser)),
});

export default connect(null, mapDispatchToProps)(Login);

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your problem is on getUser, you are resolving promise passing a state variable (currentUser) that may have been not setted (because setCurrentUser is async). I would suggest to modify you code in this way:

  const getUser = new Promise((resolve, reject) => {
    console.log('doing...');
    firebase.auth().onAuthStateChanged(firebaseUser => {
      setCurrentUser(firebaseUser);
      resolve(firebaseUser);
      console.log(firebaseUser);
    });
  });
about 4 years ago · Juan Pablo Isaza Relatório

0

export const login = ({ currentUser }) => ({
  type: UserType.USER_LOGIN,
  payload: currentUser,
});

should be -

export const login = (currentUser) => ({
  type: UserType.USER_LOGIN,
  payload: currentUser,
});

i.e no destructuring of currentUser

OR

googleLogin: currentUser => dispatch(login(currentUser))

should be

googleLogin: currentUser => dispatch(login({currentUser}))
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