recompose icon indicating copy to clipboard operation
recompose copied to clipboard

the componentDidMount method write in lifecycle can't access props

Open cexoso opened this issue 9 years ago • 5 comments

how can I call method in props?

cexoso avatar May 17 '17 08:05 cexoso

Please try again. Try to explain more clear what you really want.

istarkov avatar May 17 '17 09:05 istarkov

you can access props through this.props in componentDidMount in lifecycle

Isaddo avatar May 17 '17 11:05 Isaddo

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)

cexoso avatar May 17 '17 13:05 cexoso

Hi,

try to change:

componentDidMount: () => {
}

for this:

componentDidMount () {
}

carlosvillu avatar May 17 '17 13:05 carlosvillu

Thank you. I make a stupid mistakes.

cexoso avatar May 18 '17 02:05 cexoso