What are react components?

Components are the building blocks of any React app and a typical React app will have many of these. Simply put, a component is a JavaScript class or function that optionally accepts inputs i.e. properties(props) and returns a React element that describes how a section of the UI (User Interface) should appear.

Type of components?

  • Stateless Components
  • Stateful component

What is stateless components?

stateless component is just a plain JavaScript function which takes props as an argument and returns a react element. The functional component is also known as function components

What is meaning of stateless components?

A stateless component has no state, it means that you can’t reach `this.state` inside it. It also has no lifecycle so you can’t use componentDidMount and other hooks.

We have two way to create function component

Simple Example

function Greeting() {
  const message = 'This is a Function Component!';

  return <h1>{message}</h1>;
}

Example with Props 

function Greeting(props) {
  return <h1>Hello {props.name}!</h1>;
}

Arrow function

var Greeting = (props) => {
    return <h1>Hello {props.name}!</h1>;
}

Stateful Component

We have two way to write statefull component

  • React createClass Components
  • Class Components

createClass Components

Everything started out with React’s createClass Components. The createClass method provided developers with a factory method to create React class components without using a JavaScript class. It was the status quo for creating React components prior JavaScript ES6, because in JavaScript ES5 there was no class syntax available:

var Greeting = React.createClass({  
    render: function() {
        return (
            <h1>Hello {this.props.name}!</h1>
        );
    }
 })

Class Components

We can also write Components using ES6 classes. Component class is a bit more complicated. It has a state, lifecycle hooks and it is a javascript class which means that React creates instances of it. React should initialise the component class in order to call lifecycle hooks, call a constructor, initialise state and more.

Simple Example

class Greeting extends React.Component {
    render() {
        return <h1>Hello {this.props.name}!</h1>
    }
 }

Click here to learn more about react component lifecycle

If you have found this article useful, share this article with your friends and leave comment if you have any question. Your responses are also highly appreciated.

10 Comments

  1. cbd for dogs said:

    I wanted to put you the tiny remark to be able to thank you over again on your pretty principles you’ve featured in this article. It is quite seriously generous of people like you to give openly just what many individuals might have marketed as an e book to generate some dough for themselves, most notably given that you could possibly have done it if you ever decided. These principles as well served to be a fantastic way to fully grasp other people have similar fervor much like my own to know the truth many more pertaining to this issue. I am certain there are millions of more fun opportunities in the future for individuals who looked over your blog post. https://nfesusmmrn.online/

    January 6, 2021
    Reply
  2. payday loan said:

    I’m just commenting to let you be aware of of the extraordinary discovery my cousin’s daughter enjoyed going through your site. She discovered lots of issues, most notably how it is like to have a very effective giving mindset to have many others just gain knowledge of certain extremely tough matters. You really did more than our desires. Many thanks for distributing the important, healthy, educational and as well as easy thoughts on the topic to Gloria. https://zujepopoem.online/

    January 7, 2021
    Reply
  3. cheap viagra said:

    I intended to create you a little note just to give thanks as before for all the exceptional guidelines you’ve discussed at this time. It has been certainly incredibly generous of people like you to allow extensively all that a lot of folks could have distributed as an e-book in order to make some bucks for their own end, chiefly considering the fact that you could have done it if you considered necessary. The good ideas additionally acted as the easy way to be certain that most people have a similar dream just like mine to find out a little more in regard to this condition. I’m certain there are lots of more enjoyable moments ahead for individuals that examine your blog. https://gemerekliler.com/

    January 7, 2021
    Reply
  4. tadalafil tablets said:

    I am also commenting to let you know of the helpful encounter my friend’s daughter found reading through your web page. She figured out numerous issues, not to mention what it’s like to have an amazing giving mood to have most people really easily know selected impossible subject areas. You really exceeded our own expectations. Many thanks for imparting the invaluable, safe, explanatory and even easy thoughts on the topic to Janet. https://hillsdaledowcenter.com/

    January 7, 2021
    Reply
  5. order propecia said:

    I am commenting to let you know of the fabulous encounter my cousin’s daughter obtained using your web site. She noticed so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn specific specialized things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth. https://harrington-service.com/

    January 8, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *