Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

How to access props from lifecycle? #356

Closed
ChenLi0830 opened this issue Apr 11, 2017 · 20 comments
Closed

How to access props from lifecycle? #356

ChenLi0830 opened this issue Apr 11, 2017 · 20 comments

Comments

@ChenLi0830
Copy link

ChenLi0830 commented Apr 11, 2017

In my current component, the lifecycle methods call methods in props. For example,

      componentWillMount() {
        this.props.onEnterScreen();
      }
  
      componentDidMount() {
        this.props.toggleModal();
      }

Now I want to rewrite it using recompose's lifecycle as shown below, how do I access the props?

lifecycle({
      componentWillMount() {
        props.onEnterScreen(); // <-- props is not defined
      },
})
@TrySound
Copy link
Contributor

@ChenLi0830 Same way. All methods just extend component class and use it context. It's not quite clean and will be deprecated some day.

@ChenLi0830
Copy link
Author

Thank you @TrySound, do you mean I can access the props using this.props?

@TrySound
Copy link
Contributor

Yep

@TrySound
Copy link
Contributor

@istarkov We need to update docs

@timkindberg
Copy link
Contributor

Please do not deprecate lifecycle! I use it quite often.

@ChenLi0830
Copy link
Author

It worked, thanks a lot @TrySound!
Closing this issue.

@nick121212
Copy link

it worked,thanks a lot @TrySound
lifecycle({ componentDidMount: ()=> { this.props.getList(this.props); } } not worked yet

@wuct
Copy link
Contributor

wuct commented Apr 17, 2017

@nick121212 because this in an arrow function doesn't refer to the class instance. This behavior is from ECMAScript.

@iamdanthedev
Copy link

Thanks @wuct , I somehow missed this fact and was struggling for 15 minutes %)

@shawnmmatthews
Copy link

I'm getting undefined on

export const fetchOptions= lifecycle({  
  componentDidMount() {
   this.props.dispatch(change('form', 'test', this.props.test)); 
  }
});

Any thoughts?

@TrySound
Copy link
Contributor

@shawnmmatthews Just use class.

@shawnmmatthews
Copy link

@TrySound, Thanks for the response. Just confirming you are saying just use a class on the markup component vs trying to handle this in the HOC

@dmitryrn
Copy link

dmitryrn commented Jun 21, 2018

For use this in lifeCyrcle method you need to write like this:

lifecycle({
  componentDidMount() { this.props.some /* YES */ }
  // `this` is binded to wrapper class component
})

Instead of:

lifecycle({
  componentDidMount: () => { this.props.some /* NO */ }
})

Arrow function using parent's context.

@mauroporras
Copy link
Contributor

I agree with @TrySound, at that point is seems better just to use a class.

@LaloHao
Copy link

LaloHao commented Aug 8, 2018

The class would imply all the noise of binding "this" to all handlers + "this.state" in the constructor + props splicing in the render method though

@TrySound
Copy link
Contributor

TrySound commented Aug 8, 2018

@LaloHao And how lifecycle HOC solves any of it? It's just a wrapper around class with all class problems.
https://github.com/acdlite/recompose/blob/master/src/packages/recompose/lifecycle.js

@LaloHao
Copy link

LaloHao commented Aug 8, 2018 via email

@webberwang
Copy link

webberwang commented Dec 14, 2018

So how do you access props using a stateless component?

Found this package https://www.npmjs.com/package/@hocs/with-lifecycle

@mauroporras
Copy link
Contributor

mauroporras commented Dec 15, 2018

@webberwang
https://medium.com/@PhilipAndrews/react-how-to-access-props-in-a-functional-component-6bd4200b9e0b

Basically like this:

const Child = (props) => {
  return (
    <div style={{backgroundColor: props.eyeColor}} />
  )
}

@webberwang
Copy link

@webberwang
https://medium.com/@PhilipAndrews/react-how-to-access-props-in-a-functional-component-6bd4200b9e0b

Basically like this:

const Child = (props) => {
  return (
    <div style={{backgroundColor: props.eyeColor}} />
  )
}

Thanks for that, I forgot to clarify in the lifecycle block. But I did find this package that integrates nicely with recompose which does that

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests