S
S
Smuzzzzi2019-01-19 11:26:17
React
Smuzzzzi, 2019-01-19 11:26:17

How to connect mapDispatchToProps?

I did it like this, but I still need to pass login to connect how to do it?

import { hideModal, showModal, login } from '../../actions';

const mapStateToProps = state => ({
  isAuthenticated: !!state.user.token
});

const mapDispatchToProps = dispatch => ({
  handleClose: () => {
    dispatch(hideModal());
  },
  showRegister: () => {
    dispatch(showModal('REGISTER', {}));
  },
  showForgot: () => {
    dispatch(showModal('FORGOT', {}));
  },
});

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(LoginModal);

If you do this it will throw an error
export default connect(
  mapStateToProps,
  mapDispatchToProps,
  { login }
)(LoginModal);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
holymotion, 2019-01-19
@holymotion

import { hideModal, showModal, login } from '../../actions';

const mapStateToProps = state => ({
  isAuthenticated: !!state.user.token
});

const mapDispatchToProps = dispatch => ({
  handleClose: () => {
    dispatch(hideModal());
  },
  showRegister: () => {
    dispatch(showModal('REGISTER', {}));
  },
  showForgot: () => {
    dispatch(showModal('FORGOT', {}));
  },
  login: () => {
   dispatch(login('....'))
  }
});

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(LoginModal);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question