MaciejF
MaciejF

Reputation: 546

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?

Upvotes: 2

Views: 4129

Answers (2)

8bitme
8bitme

Reputation: 947

In the alpha version of React there is an opt in feature called hooks which will address this. See more on hooks here https://reactjs.org/docs/hooks-overview.html

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816384

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

Upvotes: 9

Related Questions