0 votes
in ReactJS by

What are pure functional Components?

1 Answer

0 votes
by

Traditional React Components as we have seen so far are making a class with class Example extends React.Component or React.createClass(). These make stateful components on the off chance that we at any point set the state (i.e. this.setState(), getInitialState(), or this.state = {} inside a constructor()). 

In the event that we have no expectation for a Component to require state, or to require lifecycle methods, we can really compose Components with a pure function, consequently the expression "pure function Component":

1

2

3

4

5

6

7

8

9

10

function Date(props)

{

let {msg="The date is:"} = props

let now = new Date()

return <div>

<span> {msg}</span>

<time> {now.toLocaleDateString()}</time>

</div>

}

This function that returns a React Element can be used wherever we see fit:

1

DOM.render(<div> <Date msg="Today is"/></div>)

You might notice that also takes a prop – we can still pass information into the Component.

Related questions

0 votes
asked Dec 8, 2020 in ReactJS by SakshiSharma
0 votes
asked Sep 7, 2023 in VueJS by DavidAnderson
...