Dispatching an action from a Redux container without extending React.Component

Viewed 1868

I have a container component within my React and Redux application:

import { connect } from 'react-redux'

import MyComponent from '../components/mycomponent'

const mapStateToProps = state => ({
  myData: state.myData[state.activeDataId]
})

export default connect(mapStateToProps)(MyComponent)

If state.myData[state.activeDataId] does not exist then I want to dispatch an action to fetchMyData or fetchMyDataIfNeeded.

Note that, at the moment, my container does not contain any JSX, it just forwards props to a presentational component. I have seen this being called a 'Pure Container' though I'm not sure if that's a common term.

Is there a common pattern to dispatch actions from a Pure Container? I am thinking without:

  • expecting the presentational component to worry about this logic by passing an onLoad event to it
  • making the container a React.Component and triggering via componentDidMount

Is it a bad idea to dispatch actions from mapStateToProps, mapDispatchToProps or mergeProps?

3 Answers
Related