izzyp
izzyp

Reputation: 173

Type error when calling mapped action - mapping actions to props react/redux

I am trying to map an action to props however however I'm getting an error: TypeError: _this2.props.updateUsername is not a function

How does one successfully map redux actions to props and call the function successfully? I havnt seen this error pop up in any other stackoverflow question/answers is it a simple mistake? Could it be a wrong setup of redux in .index or .app?

I have tried: - importing without using default export - having different formats of mapDispatchToProps (eg without using bindactioncreators) - fixing typos

Reducers:

import { UPDATE_USERNAME} from '../actions/user-actions'

export function passReducer(state = "", {type, payload}) {
  switch (type) {
    case true:
      return payload
    default:
      return state
  }
}

export function usernameReducer(state = '', {type, payload}) {
  switch (type) {
    case UPDATE_USERNAME:
      return payload.username
    default:
      return state
  }
}

export default { passReducer, usernameReducer };

Many Thanks

Upvotes: 1

Views: 390

Answers (2)

Faisal Arshed
Faisal Arshed

Reputation: 539

Don't use mapDispatchToProps. Instead just wrap all the actions you want to map inside an object and pass them as the second argument to the connect helper method.

Like this connect(mapStateToProps, { updateUsername })(UserLoginPage)

Hope this helps!

Upvotes: 1

Harish
Harish

Reputation: 1911

Can you check once after updating your constructor as below?

constructor(props) {
    super(props);
    //...
  }

Upvotes: 1

Related Questions