In the modern era, components are the building blocks of an Angular or React project. For Angular projects, there is only one way of defining the components, so it is not a big task to learn the syntax and uses of components. But if we talk about React, there are multiple ways to define a component's syntax. So sometimes, it becomes difficult to remember the syntax of the component and understand the uses of components for specific scenarios.
In React, we can create either a class-based component or a function-based component. So, in this article, we will learn many methods to create a component and will also learn, in which case, what method will be suitable.
Create Component using Class
- import React from 'react';
- import PropTypes from 'prop-types';
-
- class Main extends React.Component {
- constructor(props) {
- super(props);
-
- this.state = {
- title: "Hello React !!!",
- }
- }
- render() {
- return (
- <div>
- <h2>
- {this.state.title}
- </h2>
- <p>User Name: {this.props.name}</p>
- <p>User Age: {this.props.age}</p>
- </div>
- );
- }
-
- }
- Main.propTypes ={
- name:PropTypes.string.isRequired,
- age:PropTypes.number.isRequired
- }
-
- Main.defaultProps = {
- name: 'Pankaj Kumar Choudhary',
- age: 24
- };
-
- export default Main;
This is the default method to create a component. In this method, we inherit the “React.Component” class and define the render method. Render method returns the View content for the component. This is mostly used to create the components in a React application. The above component's syntax is allowed for “.jsx” syntax.
Stateless Functional Component
- import React from 'react';
- import PropTypes from 'prop-types';
-
- function Main(props) {
- const myFun=()=>{
- alert('This is stateless functional component');
- }
- return( <div>
- <h2>
- Hello React!!!
- </h2>
- <p>User Name: {this.props.name}</p>
- <p>User Age: {this.props.age}</p>
- <input type="button" value="Submit" onClick={myFun} />
- </div>)
- };
- export default Main;
The above-given code is the best example of a stateless functional component. Stateless Functional Component is the best approach if we don’t need to define the state for our component. If we are not using the lifecycle hooks or state in component, then stateless functional component can be the best approach.
Stateless Functional Component using lambda expression
- import React from 'react';
- import PropTypes from 'prop-types';
-
- var Main=(props)=> {
- const myFun=()=>{
- alert('This is stateless functional component');
- }
- return( <div>
- <h2>
- Hello React!!!
- </h2>
- <p>User Name: {this.props.name}</p>
- <p>User Age: {this.props.age}</p>
- <input type="button" value="Submit" onClick={myFun} />
- </div>)
- };
- export default Main;
The above example is the same as the previous one. The only difference is that in this example, we define the function using lambda expression.
The Presentational Component
The Presentational components are components that render only HTML. The Presentational components rarely have a state. These components only define how the View will look and they don’t deal with state and store (if Redux is used). These components don’t define how data is loaded or mutated into component.
- import React from 'react';
- import PropTypes from 'prop-types';
-
- var Main=(props)=> (
- <div>
- <h2>
- Hello React!!!
- </h2>
- <p>User Name: {this.props.name}</p>
- <p>User Age: {this.props.age}</p>
- <input type="button" value="Submit" onClick={myFun} />
- </div>
- );
- export default Main;
Define Component using “createClass” Method
This is the oldest way to define the component. It is similar to “React.Component” but “React.component” is small syntax sugar that allows us to create components using the ES6 syntax. If you are not using the ES6, then “createClass” is the best method to create a component because each ES6 syntax finally converts into ES5 syntax.
- var Main = React.createClass {
- render() {
- return (
- <div>
- <h2>
- {this.state.title}
- </h2>
- <p>User Name: {this.props.name}</p>
- <p>User Age: {this.props.age}</p>
- </div>
- );
- }
-
- }
- Main.propTypes ={
- name:PropTypes.string.isRequired,
- age:PropTypes.number.isRequired
- }
-
- Main.defaultProps = {
- name: 'Pankaj Kumar Choudhary',
- age: 24
- };
Define Component using TSX
If you are using the TSX(TypeScript Syntax XML) instead of JSX(JavaScript Syntax XML) to create the component, then the following will be the syntax.
- import * as React from 'react';
- import { Link } from 'react-router-dom';
-
- interface ComponentState{
- paging:number,
- order:string
- }
-
- interface ComponentProp{
- Paging:Function,
- Order:Function
- }
-
- export default class SortPaging extends React.Component<ComponentProp, ComponentState>
- {
- constructor(props:ComponentProp){
- super(props);
- this.state={paging:20,order:'Default Sorting'}
- }
-
- ordering=()=>{
-
- }
-
- render() {
- return (
- <div className="product_sorting_container product_sorting_container_top">
- <p>This is React Component</p>
- </div>
- );
- }
- }
There is no big difference between the syntax of JSX and TSX to create a component. The only difference is that in “React.Component”, we need to pass the two parameter components' initial props and states. If we don’t have any initial state and props for the component, then we can pass any value type for both values. Believe me, TSX provides more features rather than simple JSX syntax. Using TSX, we can get the benefits of TypeScript programming language.
Points to consider when creating the Components
- If you need to define the component life cycle hooks and state for the component, then “React.Component” class syntax will be a better way to create the component for the project.
- If we are not using the state and lifecycle hooks for the component, then functional component can increase the performance of our application; so in this case, instead of class, we can use the functional component.
- If you are using the JSX for React structure, then I believe, use the TSX instead of JSX. This is my personal opinion because I think if we use TSX, then we get the TypeScript power along with all JSX features.
- Stateless functional component and Presentational component both are functions but the only difference is the syntax to write the component.
- If there is any need of the component lifecycle hooks and state of the component, then class will be a better approach to create a component.
Conclusion
In this article, we learned different techniques to write the components in React application. Put your query and doubts in the comment section if you have any. Thanks!