Categories

Nov 26, 2019 in React JS
Q:

What are pure functional Components?

1 Answer

Nov 26, 2019

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.

Click here to read more about React JS
Click here to read more about Insurance

Related questions

Madanswer
Mar 30 in React JS
Jul 2, 2019 in React JS
May 20 in React JS
...