A Quick Introduction To React - Js
A Quick Introduction To React - Js
a-quick-intro-to-reactjs
A quick introduction to React.js
React is a JavaScript library for building user interfaces. It was developed by Facebook and allows developers to
create large web applications where data can change without reloading the page. React is component-based, meaning
the UI is divided into independent, reusable pieces of code.
Prerequisites
You should be familiar with:
HTML
CSS
JavaScript (ES6+)
Basics of Node.js and npm/yarn for package management
Setup
First, ensure you have Node.js and npm installed. If not, download it from Node.js official site.
cd my-react-app
npm start
Project Structure
public/ : Contains static files like index.html .
src/ : The main folder where your components, styles, and logic reside.
App.js : The main component, acting as the entry point of your app.
1. Functional Component
1/6
a-quick-intro-to-reactjs
// src/components/Greeting.js
import React from 'react';
function Greeting() {
return <h1>Hello, welcome to React!</h1>;
}
// src/App.js
import React from 'react';
import './App.css';
import Greeting from './components/Greeting';
function App() {
return (
<div className="App">
<Greeting />
</div>
);
}
Example:
Handling Events
React handles events similarly to DOM elements but uses camelCase for event names and the event handler should
be passed as a function reference.
// src/components/Button.js
import React from 'react';
function Button() {
function handleClick() {
alert('Button clicked!');
}
2/6
a-quick-intro-to-reactjs
return <button onClick={handleClick}>Click Me!</button>;
}
// src/App.js
import React from 'react';
import './App.css';
import Greeting from './components/Greeting';
import Button from './components/Button';
function App() {
return (
<div className="App">
<Greeting />
<Button />
</div>
);
}
// src/components/Counter.js
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
Add it to App.js :
// src/App.js
import React from 'react';
import './App.css';
import Greeting from './components/Greeting';
import Button from './components/Button';
import Counter from './components/Counter';
function App() {
return (
<div className="App">
3/6
a-quick-intro-to-reactjs
<Greeting />
<Button />
<Counter />
</div>
);
}
Now, each time you click the "Increment" button, the counter will increase.
Example:
// src/components/User.js
import React from 'react';
Now use the User component in App.js and pass a name as a prop:
// src/App.js
import React from 'react';
import './App.css';
import Greeting from './components/Greeting';
import Button from './components/Button';
import Counter from './components/Counter';
import User from './components/User';
function App() {
return (
<div className="App">
<Greeting />
<Button />
<Counter />
<User name="Alice" />
<User name="Bob" />
</div>
);
}
In this example, the User component receives the name prop and renders it.
Conditional Rendering
React allows you to conditionally render components based on the state or props.
Example:
4/6
a-quick-intro-to-reactjs
// src/components/Status.js
import React, { useState } from 'react';
function Status() {
const [loggedIn, setLoggedIn] = useState(false);
return (
<div>
{loggedIn ? <h2>Welcome Back!</h2> : <h2>Please log in.</h2>}
<button onClick={() => setLoggedIn(!loggedIn)}>
{loggedIn ? 'Log out' : 'Log in'}
</button>
</div>
);
}
// src/App.js
import React from 'react';
import './App.css';
import Greeting from './components/Greeting';
import Button from './components/Button';
import Counter from './components/Counter';
import User from './components/User';
import Status from './components/Status';
function App() {
return (
<div className="App">
<Greeting />
<Button />
<Counter />
<User name="Alice" />
<User name="Bob" />
<Status />
</div>
);
}
Conclusion
You've now learned the basics of React, including:
Setting up a project
Creating functional components
Using JSX
Handling events
Managing state with hooks
Passing props between components
Conditional rendering
5/6
a-quick-intro-to-reactjs
From here, you can start exploring more advanced topics like routing with React Router, state management with
Redux, and styling with CSS-in-JS libraries.
If you want to further customize and extend this project, consider adding:
Let me know if you need any help with more advanced topics!
Reference
https://maduranga.com/a-quick-intro-to-reactjs
6/6