React JS Introduction
24 April 2025 | Category: React Js
Welcome to this beginner-friendly guide on React JS! Whether you’re stepping into web development or aiming to create dynamic user interfaces, React is an excellent starting point. In this tutorial, we’ll introduce the core concepts of React and help you build a simple “Welcome Card” app that displays a personalized message. By the end, you’ll be well-equipped to explore more advanced React features.
What is React JS?
React JS is an open-source JavaScript library developed by Facebook for building fast, responsive user interfaces, especially for single-page applications (SPAs). It allows developers to build reusable UI components that react to data changes efficiently.
Why Choose React?
- Component-Based: Build UIs from independent, reusable components.
- Efficient Rendering: React uses a virtual DOM to minimize DOM manipulations, boosting performance.
- Strong Community: A rich ecosystem of libraries, tools, and job opportunities.
- Versatile: Works for web, mobile (React Native), and even VR applications.
Prerequisites
To follow along, you should have:
- Basic understanding of HTML, CSS, and JavaScript (ES6+ features).
- Node.js and npm installed (download from nodejs.org).
- A code editor like VS Code.
Setting Up a React Project
We’ll use Create React App to set up our project quickly.
Step 1: Install Node.js
Download and install Node.js if you haven’t already. It comes bundled with npm.
Step 2: Create the React App
Open your terminal and run:
npx create-react-app welcome-card
cd welcome-card
npm start
Your browser will open http://localhost:3000 showing the default React page.
Step 3: Understand the Project Structure
Key folders and files:
src/
: Your app’s source code (e.g., App.js, index.js)public/
: Static assets like index.htmlpackage.json
: Manages dependencies and scripts
Core Concepts in React
1. Components
Components are the heart of a React app. They are JavaScript functions that return JSX:
function Greeting() {
return <h1>Hello, React!</h1>;
}
2. JSX (JavaScript XML)
JSX lets you write HTML-like syntax directly within JavaScript:
const element = <p>Welcome to React!</p>;
JSX gets compiled into React.createElement
calls under the hood.
3. Rendering
React renders your components using ReactDOM.render()
:
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
Build: Welcome Card Application
Let’s make a simple personalized welcome card.
Step 1: Clean the App
In src/App.js
, replace the content with:
function App() {
return (
<div>
<h1>Welcome to My React App</h1>
</div>
);
}
export default App;
Delete App.css
, logo.svg
, and other unused files. Add the following to index.css
:
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
Step 2: Create WelcomeCard Component
Create a new file WelcomeCard.js
in src/
and add:
function WelcomeCard({ name, message }) {
const showAlert = () => alert(`Hello, ${name}!`);
return (
<div style={{
backgroundColor: '#fff',
padding: '20px',
borderRadius: '8px',
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
textAlign: 'center',
width: '300px'
}}>
<h2>Welcome, {name}!</h2>
<p>{message}</p>
<button onClick={showAlert}>Greet</button>
</div>
);
}
export default WelcomeCard;
Step 3: Use the Component in App.js
Update App.js
:
import WelcomeCard from './WelcomeCard';
function App() {
return (
<div>
<WelcomeCard name="Alice" message="Thanks for learning React!" />
<WelcomeCard name="Bob" message="Keep coding and have fun!" />
</div>
);
}
export default App;
Step 4: Run the App
Make sure your development server is running:
npm start
Visit http://localhost:3000 to see two welcome cards with greetings.
Code Breakdown
- App Component: Main entry point that renders multiple WelcomeCard components.
- WelcomeCard Component: A reusable UI card displaying user data via props.
- Props: Enable passing custom values to components.
- JSX: Makes writing UI inside JavaScript simple and intuitive.
- Inline Styling: Used here for quick setup (can be replaced with CSS or styled-components).
What’s Next?
Now that you’ve got the basics down:
- Learn about
useState
to handle component state. - Practice event handling (e.g., button clicks).
- Build projects like counters, forms, or to-do apps.
- Explore tools like React Router or Tailwind CSS.
Practice Challenge
Add a button to the WelcomeCard that triggers an alert like:
alert(`Hello, ${name}!`);
Additional Resources
Happy Coding and welcome to the React world!