Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement lifecycle methods in functional components in React?

For a simple component written like this:

import React from 'react'

const MyComponent = ({text}) => {
    return(<div> {text} </div>
}

Is there a syntax for implementing lifecycle method like componentDidMount() or do I have to convert code to React.Component class?

like image 935
MaciejF Avatar asked Aug 30 '26 03:08

MaciejF


1 Answers

Is there a syntax for implementing lifecycle method like componentDidMount()

No.

do I have to convert code to React.Component class?

Yes.

See https://facebook.github.io/react/docs/state-and-lifecycle.html

like image 197
Felix Kling Avatar answered Sep 01 '26 09:09

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!