Section 2_Unit 2 React JS
Section 2_Unit 2 React JS
Advantages of React
1. Speed
2. Flexibility
3. Performance
4. Reusable Components
5. It helps to build rich user interfaces
React Directly in HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="mydiv"></div>
<script type="text/babel">
function Hello() {
return <h1>Hello World!</h1>;
}
const container = document.getElementById('mydiv');
const root = ReactDOM.createRoot(container);
root.render(<Hello />)
</script>
</body>
</html>
Setting up a React Environment
• If you have npx and Node.js installed, you can create a React
application by using create-react-app
• Step 1:
• Run this command to create a React application named my-react-
app
• The create-react-app will set up everything you need to run a
React application.
• Step 2:
• Run the React Application
• Run this command to move to the my-react-app directory:
• Step 3:
• Run this command to run the React application my-react-app:
• A new browser window will pop up with your newly created React
App.
• If not, open your browser and type localhost:3000 in the address bar.
• The result:
Modify the React Application
• Look in the my-react-app directory, and you will find a src folder.
• Inside the src folder there is a file called App.js, open it and it will look like this:
• Replace all the content inside the <div className="App"> with a <h1> element.
function App() {
return (
<div className="App">
<h1>Hello World!</h1>
</div>
);
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom/client’;
❑ Coding JSX
• JSX allows us to write HTML elements in JavaScript and place them in
the DOM without any createElement() and/or appendChild()
methods.
• JSX converts HTML tags into react elements.
With JSX
import React from 'react';
import ReactDOM from 'react-dom/client';
JSX follows XML rules, and therefore HTML elements must be properly closed.
const x = 5;
let text = "Goodbye";
if (x < 10) {
text = "Hello";
}
const myElement = <h1>{text}</h1>;
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(myElement);
Conditions - if statements (Use ternary expressions)
❑With Setting up a React Environment
const x = 5;
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(myElement);
React Components
• Components are like functions that return HTML
elements.
Class Component
• A class component must include the extends React.Component statement.
This statement creates an inheritance to React.Component, and gives your
component access to React.Component's functions.
• The component also requires a render() method, this method returns HTML.
❑Create a Class component called Car
function Car() {
return <h2>Hi, I am a Car!</h2>;
}
Rendering a Component
• React application has a component called Car, which returns an <h2> element.
• To use this component in your application, use similar syntax as normal HTML: <Car />
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car() {
return <h2>Hi, I am a Car!</h2>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Car />);
Props
• Components can be passed as props, which stands for properties.
function Car(props) {
return <h2>I am a {props.color} Car!</h2>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Car color="red"/>);
Components in Components
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car() {
return <h2>I am a Car!</h2>;
}
function Garage() {
return (
<>
<h1>Who lives in my Garage?</h1>
<Car />
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
React Events
• React events are written in camelCase syntax:
function MissedGoal() {
return <h1>MISSED!</h1>;
}
function MadeGoal() {
return <h1>GOAL!</h1>;
}
function Goal(props) {
const isGoal = props.isGoal;
if (isGoal) {
return <MadeGoal/>;
}
return <MissedGoal/>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Goal isGoal={false} />);
React Lists
• The JavaScript map() array method is generally the preferred method.
import React from 'react';
import ReactDOM from 'react-dom/client';
function Car(props) {
return <li>I am a { props.brand }</li>;
}
function Garage() {
const car = ['Ford', 'BMW', 'Audi'];
return (
<>
<h1>Who lives in my garage?</h1>
<ul>
{cars.map((car) => <Car brand={car} />)}
</ul>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Garage />);
Styling React Using CSS
• Inline Styling
import React from 'react';
import ReactDOM from 'react-dom/client';
const Header = () => {
return (
<>
<h1 style={{color: "red"}}>Hello Style!</h1>
<p>Add a little style!</p>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Header />);
camelCased Property Names
• Use backgroundColor instead of background-color:
import React from 'react';
import ReactDOM from 'react-dom/client';