Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 1.3 KB

20170921.md

File metadata and controls

54 lines (42 loc) · 1.3 KB

ReactJS

Useful links

Tips

Create a Route for your module that connects to the redux store

import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import { connect } from 'react-redux'

class ModuleRoute extends React.Component {
  componentWillMount () {
    const { dispatchBeforeMount } = this.props

    // this function is injected in the component using connect
    dispatchBeforeMount()
  }

  render () {
    // model comes from the store
    const { component: Component, model, ...rest } = this.props

    return (
      <Route {...rest} render={() => {
          return <Component {...this.props} />
      }} />
    )
  }
}

const stateToProps = ({moduleReducer}) => ({
  model: moduleReducer.model
})

const dispatchToProps = dispatch => {
  return {
    logout: () => {
      dispatch({type: 'DESTROY_SOMETHING'})
    },
    dispatchBeforeMount: () => {
      dispatch({type: 'GET_SOMETHING'})
    }
  }
}

export default connect(stateToProps, dispatchToProps)(ModuleRoute)