Unir 2 - 2. Components in React
Unir 2 - 2. Components in React
Contents
• Your first React Application
• Setting up the development environment
• What is a Component?
• Building Product
• Updating state and immutability
• Components:
• A time-logging app
• Building a static version of the app
• determining stateful components and initial states
• add inverse data flow, update and delete timers
What is ReactJS?
• ReactJS is a declarative, efficient, and flexible JavaScript library for building
reusable UI components.
• It is an open-source, component-based front end library which is responsible only for the
view layer of the application. It was initially developed and maintained by Facebook and
later used in its products like WhatsApp & Instagram.
• ReactJS is a simple, feature rich, component based JavaScript UI library. It can be used to
develop small applications as well as big, complex applications. ReactJS provides minimal
and solid feature set to kick-start a web application.
• React community compliments React library by providing large set of ready-made
components to develop web application in a record time.
• React community also provides advanced concept like state management, routing, etc., on
top of the React library.
What is ReactJS?
ReactJS uses virtual DOM based mechanism to fill in data (views) in HTML
DOM.
The virtual DOM works fast owning to the fact that it only changes
individual DOM elements instead of reloading complete DOM every time
A React application is made up of multiple components, each responsible
for outputting a small, reusable piece of HTML. Components can be nested
within other components to allow complex applications to be built out of
simple building blocks.
A component may also maintain internal state - for example, a TabList
component may store a variable corresponding to the currently open tab.
Why we use ReactJS?
• The initial version, 0.3.0 of React is released on May, 2013 and the
latest version, 17.0.1 is released on October, 2020.
• The major version introduces breaking changes and the minor
version introduces new feature without breaking the existing
functionality.
Features, Benefits and Applications
• Features • Benefits • Applications
• The salient features • Few benefits of
of React library are • Few popular websites powered
using React library are as by React library are listed below −
as follows − follows −
• Solid base • Easy to learn • Facebook, popular social media
application
architecture • Easy to adept in modern
as well as legacy • Instagram, popular photo
• Extensible sharing application
architecture application
• Faster way to code a • Netflix, popular media
• Component based streaming application
library functionality
• Availability of large • Code Academy, popular online
• JSX based design number of ready-made training application
architecture component • Reddit, popular content sharing
• Declarative UI • Large and active application
library community
The CLI Tools
• React provides CLI tools for the developer to fast forward the creation,
development and deployment of the React based web application.
• React CLI tools depends on the Node.js and must be installed in your
system.
• If you have installed Node.js on your machine, check it using the below
command −
• node --version
• You could see the version of Nodejs you might have installed.
• My machine has the following version:
ReactDOM.render(
<Welcome />,
document.getElementById("root")
);
Two Types of Components
• Stateless Functional Components
• Stateful Class Components
function Car(props) {
return <h2>I am a { props.brand }!</h2>;
}
function Garage() {
return (
<>
<h1>Who lives in my garage?</h1>
<Car brand="Ford" />
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
2. Send the variable name
function Car(props) {
return <h2>I am a { props.brand }!</h2>;
}
function Garage() {
const carName = "Ford";
return (
You can also send a
<> variable name
<h1>Who lives in my garage?</h1>
<Car brand={ carName } />
</>
);
}
function Car(props) {
return <h2>I am a { props.brand.model }!</h2>;
}
function Garage() {
const carInfo = { name: "Ford", model: "Mustang" };
return (
<>
<h1>Who lives in my garage?</h1>
<Car brand={ carInfo } />
</>
);
}
// Child Component
class Child extends React.Component{
render(){
return(
<div>
<h2>Hello, {this.props.name}</h2>
<h3>You are inside Child Component</h3>
<h3>Your user id is: {this.props.userId}</h3>
</div>
); } }
ReactDOM.render(
// passing props
<Parent />,
document.getElementById("root")
);
import React from 'react';
import ReactDOM from 'react-dom';
Can we pass default values using props?
// Component
class ExampleClass extends React.Component {
render() { • Yes, React provides us with
return (
<div> something called defaultProps for
{/* using default prop - title */}
<h1>This is {this.props.title}'s Website!</h1>
this purpose.
</div>
); • defaultProps:
}
}
• The defaultProps is a method that we
can use to store as much information
// Creating default props for as we want for a particular class, and
// ExampleClass Component
ExampleClass.defaultProps = { this information can be accessed
title: “Amazon’s site" from anywhere inside that particular
}
class.
ReactDOM.render( • Every piece of information you add
<ExampleClass />,
document.getElementById("root") inside the defaultProps, will be added
); as the prop of that class.
Stateful and Stateless Components in React
• First let us look into the state
• React State is integral to using React, especially when dealing with dynamic
components
• They are the heart of stateful vs stateless React component structures
• It allows updates and rendering of components based on user input or
backend changes.
• React’s State allows for efficient data flow in an application, providing the
necessary functionality for stateful and stateless components in React to
interact seamlessly
• State in ReactJS is an attribute of a component that can change over time. It
allows a component to create dynamic and interactive experiences. This,
however, doesn’t imply that all components should have state.
• In fact, determining whether a component needs to be stateful or
stateless is a significant design decision in React application
development.
• Stateful Components
• Stateful components, as their name implies, have a state.
• They are capable of tracking changes, have knowledge of past, current, and
potential future changes in themselves, and render their output accordingly.
• Stateful components are usually class components, but with the introduction
of hooks, functional components can also maintain state.
• Stateless Components
• On the other side, stateless components are those that calculate their
internal state but do not record or modify it. Stateless components
receive data through props and render it. They are typically used for
components that merely display data and don’t manage it. To further
understand the difference between stateless and stateful
components in React, a stateless component React example could be
a simple button or a list item.
• Can you use State in all components?
• Technically, yes.
• But should you?
• Not always.
• Points to Consider:
• If a component doesn’t need to know about its past or future states, it
doesn’t need State.
• Making such a component stateful unnecessarily can lead to complicated
code and potential performance issues.
• If a component’s output doesn’t depend on its state and can be
determined solely by its props, it should be a stateless component.
• Stateless components are easier to test and understand.
• Always start building a component as stateless and only
convert it to a stateful component if necessary. This
practice makes your components more flexible and maintainable.
Comparing Stateful and Stateless
Stateful Components Stateless Components
Props Can receive props from parent components Can receive props from parent components
Re-rendering Re-renders when state or props change Re-renders when props change
More complex and can be less performant due to Simpler and can be more performant since they don’t
Performance
managing state manage state
Ideal for complex components that need to Ideal for simpler components that just render UI based on
Usage
manage state props
Create a stateful component
ReactDOM.render(
<React.StrictMode>
<Clock />
</React.StrictMode>,
document.getElementById('root')
);
Under the root folder and create index.html file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Clock</title>
</head>
<body>
<div id="root"></div>
<script type="text/JavaScript" src="./index.js"></script>
</body>
</html>