recompose
recompose copied to clipboard
the componentDidMount method write in lifecycle can't access props
how can I call method in props?
Please try again. Try to explain more clear what you really want.
you can access props through this.props in componentDidMount in lifecycle
import { connect } from 'react-redux';
import { compose, lifecycle } from "recompose"
import mainComponent from "./containers/main"
//mainComponent is a react component
import { bindActionCreators } from "react-redux"
import action from "./action"
const mapStateToProps = () => {
return { xxx: "xxx" }
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({
doSomething: action.doSomething,
}, dispatch)
}
const enhance = compose(
connect(mapStateToProps, mapDispatchToProps),
lifecycle({
componentDidMount: () => {
// I want to dispath a action here
// this does't work because this is undefined
const { xxx, doSomething } = this.props;
doSomething(xxx)
}
})
)
export default enhance(mainComponent)
Hi,
try to change:
componentDidMount: () => {
}
for this:
componentDidMount () {
}
Thank you. I make a stupid mistakes.