React Lab
React Lab
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
React Components
Components are independent and reusable bits of code. They serve the same
purpose as JavaScript functions, but work in isolation and return HTML.
Components come in two types, Class components and Function components, in this
tutorial we will concentrate on Function components.
Class Component
The component also requires a render() method, this method returns HTML.
render() {
Function Component
A Function component also returns HTML, and behaves much the same
way as a Class component, but Function components can be written
using much less code, are easier to understand
Example
function Car() {
}
Experiment No-3
The state object is where you store property values that belong to the component.
Example:
Example:
React Props
React Props are like function arguments in JavaScript and attributes in HTML.
To send props into a component, use the same syntax as HTML attributes:
Example:
function Car(props) {
Pass Data
Props are also how you pass data from one component to another, as parameters.
Example:
Send the "brand" property from the Garage component to the Car component:
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 />);
Experiment No-4
React has the same events as HTML: click, change, mouseover etc.
Adding Events
React events are written in camelCase syntax:
Example:
function Football() {
const shoot = () => {
alert("Great Shot!");
}
return (
<button onClick={shoot}>Take the shot!</button>
);
}
function Football() {
const shoot = (a) => {
alert(a);
}
return (
<button onClick={() => shoot("Goal!")}>Take the shot!</button>
);
}
function Football() {
const shoot = (a, b) => {
alert(b.type);
/*
'b' represents the React event that triggered the function,
in this case the 'click' event
*/
}
return (
<button onClick={(event) => shoot("Goal!", event)}>Take the
shot!</button>
);
}
Routing - React
Create React App doesn't include page routing.
npm i -D react-router-dom@latest
Folder Structure
To create an application with multiple page routes, let's first start with the file
structure.
Within the src folder, we'll create a folder named pages with several files:
src\pages\:
Layout.js
Home.js
Blogs.js
Contact.js
NoPage.js
Basic Usage
index.js:
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />)
Pages / Components
<Link> is used to set the URL and keep track of browsing history.
Anytime we link to an internal path, we will use <Link> instead of <a href="">.
The "layout route" is a shared component that inserts common content on all pages,
such as a navigation menu.
Layout.js:
<Outlet />
</>
)
};
Inline styling
CSS stylesheets
CSS Modules
Inline Styling
To style an element with the inline style attribute, the value must be a
JavaScript object:
#Example:
Example:
JavaScript Object
You can also create an object with styling information, and refer to it in the
style attribute:
Example:
CSS Stylesheet
You can write your CSS styling in a separate file, just save the file with
the .css file extension, and import it in your application.
App.css:
Create a new file called "App.css" and insert some CSS code in it:
body {
background-color: #282c34;
color: white;
padding: 40px;
font-family: Sans-Serif;
text-align: center;
}
index.js:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './App.css';
CSS Modules
Another way of adding styles to your application is to use CSS Modules.
CSS Modules are convenient for components that are placed in separate files.
"""Create the CSS module with the .module.css extension, example: my-
style.module.css.
Create a new file called "my-style.module.css" and insert some CSS code in it:
my-style.module.css:"""
.bigblue {
color: DodgerBlue;
padding: 40px;
font-family: Sans-Serif;
text-align: center;
}
# Car.js:
import styles from './my-style.module.css';
index.js:
import ReactDOM from 'react-dom/client';
import Car from './Car.js';