Introduction to React JS
Introduction to React JS
React.js
React.js is a JavaScript library developed
by Facebook to create user interfaces.
We use React to build single-page
applications.
In React, we can create reusable
components, making our work easier to
develop the UI.
function App() {
return <h1>Hello, React!</h1>;
}
function RedButton() {
return <button style={{ backgroundColor: 'red'
}}>Click Me</button>;
}
// src/App.jsx
import React from 'react';
import RedButton from './RedButton';
function App() {
return (
<div>
<RedButton />
<RedButton />
</div>
);
}
// src/App.jsx
import React from 'react';
function App() {
const name = 'Shaaz';
return <h1>Hello, {name}!</h1>;
}
// src/App.jsx
function App() {
return (
<div>
<h1>Hello, World!</h1>
</div>
);
}
Use a ternary operator instead of if-
else:
// src/App.jsx
function App({ isLoggedIn }) {
return
<div>{isLoggedIn ?
<h1>Welcome Back!</h1> : <h1>Please Sign
In</h1>}
</div>;
}
Use {} to embed JavaScript expressions:
// src/App.jsx
function App() {
const name = 'Shaaz';
return <h1>Hello, {name}!</h1>;
}