APSL/redux-i18n

how to pass to setLanguage to mapDispatchToProps

Closed this issue · 1 comments

hi, I believe this is very stupid question from the very beginner (which I am), but I'm stuck, so please help!

In my component which is connect via connect() I initialize mapDispatchToProps in order to get access to actions:

import * as newsActions from '../actions/news'
import * as langActions from '../actions/lang'

const mapStateToProps = state => {
  const { language } = state
  const { lang } = state.i18nState
  return { language, lang }
}

const mapDispatchToProps = dispatch => {
  return bindActionCreators(Object.assign({},
    langActions, 
    newsActions
  ), dispatch)
}

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

However if import setLanguage and add it inside Object.assign nothing happens and I don't see setLanguage in my props, so this doesn't work:

const mapDispatchToProps = dispatch => {
  return bindActionCreators(Object.assign({},
    langActions, 
    newsActions, 
    setLanguage
  ), dispatch)
}

However, I need to do it this way, I can't just use dispatch(setLanguage) because I don't have dispatch available at my props. Though if I delete mapDispatchToProps than I have dispatch available and everything works fine.

So how do I add setLanguage to my functions in mapDispatchToProps?

Oh I got it!

In case somebody is interested I just made everything in my action file:
(don't pay attention to const changeLanguage, it is my old way of changing locale in order to get the right content from API)

import { SWITCH_LANGUAGE } from './action_types.js'
import { setLanguage } from "redux-i18n"

const changeLanguage = (language) => ({type: SWITCH_LANGUAGE, language })

export const switchLanguage = (language) => (dispatch) => {
    dispatch(changeLanguage(language));
    dispatch(setLanguage(language));
}