React JS is a popular JavaScript library used for building dynamic user interfaces. It was developed by Facebook and released as open-source software in 2013. React allows developers to create interactive web applications with reusable components that can easily be updated and maintained.
In this tutorial, we will cover everything you need to know to get started with React JS, from the very basics to more advanced concepts. This is the Moye-Moye Edition, where we aim to provide a comprehensive and user-friendly guide to help you learn React in a clear and easy-to-follow manner.
Getting Started with React JS:
Before we dive into coding, let’s make sure you have the necessary tools installed on your machine. To start coding with React JS, you’ll need Node.js and npm (node package manager) installed. You can download and install Node.js from the official website (https://nodejs.org/).
Once you have Node.js installed, you can check if Node and npm are working correctly by opening a terminal or command prompt and running the following commands:
node --version
npm --version
If you see version numbers printed out for both Node and npm, then you’re good to go! Let’s move on to creating your first React app.
Creating a React App:
To create a new React app, you can use the create-react-app command-line tool provided by Facebook. Open a terminal or command prompt and run the following command to create a new React app named my-react-app:
npx create-react-app my-react-app
This command will create a new directory named my-react-app and install all the necessary dependencies to start working with React. Once the installation is complete, navigate into the my-react-app directory and start the development server by running the following command:
cd my-react-app
npm start
This will start the development server and open your new React app in a web browser. You should see a "Welcome to React" message displayed on the screen. Congratulations, you’ve successfully created your first React app!
Understanding Components in React:
In React, everything is built using components. A component is a reusable piece of code that represents a part of the user interface. Components can be nested within each other to create complex UIs. There are two types of components in React: functional components and class components.
Functional components are simple JavaScript functions that return JSX (a syntax extension for JavaScript that looks similar to HTML). Here’s an example of a simple functional component:
function Welcome() {
return <h1>Hello, world!</h1>;
}
Class components are JavaScript classes that extend React’s Component class. Here’s an example of a class component that does the same thing as the functional component above:
import React, { Component } from 'react';
class Welcome extends Component {
render() {
return <h1>Hello, world!</h1>;
}
}
Using Props in React Components:
Props (short for properties) allow you to pass data from a parent component to a child component. Props are immutable, meaning that they cannot be changed by the child component. Here’s an example of how to use props in a React component:
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
function App() {
return <Greeting name="Alice" />;
}
In this example, the App component passes the value "Alice" as a prop to the Greeting component. The Greeting component receives the prop as an argument and renders it in the output.
State in React Components:
State allows components to manage their internal data and re-render when the state changes. Unlike props, state is mutable and can be changed by the component itself. You can initialize state in a class component using the constructor method and this.state. Here’s an example of how to use state in a React component:
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
incrementCount() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
<h1>{this.state.count}</h1>
<button onClick={() => this.incrementCount()}>Increment</button>
</div>
);
}
}
In this example, the Counter component maintains internal state for the count value. When the user clicks the "Increment" button, the count value is updated using the this.setState method, which triggers a re-render of the component.
Handling Events in React Components:
In React, you can handle events like clicks, keypresses, and form submissions using event handlers. You can attach event handlers to JSX elements using props. Here’s an example of how to handle a button click event in a React component:
class ButtonClick extends Component {
handleClick() {
alert('Button clicked!');
}
render() {
return <button onClick={this.handleClick}>Click me</button>;
}
}
In this example, the handleClick method is called when the user clicks the button, triggering an alert box with the message "Button clicked!"
Conditional Rendering in React Components:
You can conditionally render elements in React based on the component’s state or props. One common way to conditionally render elements is by using JavaScript’s ternary operator. Here’s an example of conditional rendering in a React component:
function Greeting(props) {
if (props.isLoggedIn) {
return <h1>Welcome back, {props.username}!</h1>;
} else {
return <h1>Please log in</h1>;
}
}
In this example, the Greeting component renders different messages based on the value of the isLoggedIn prop. If isLoggedIn is true, the component displays a welcome message. Otherwise, it prompts the user to log in.
Using Hooks in React Components:
Hooks are a new addition to React that allow functional components to use state and other React features without writing a class. The useState and useEffect hooks are commonly used in React components to manage state and side effects. Here’s an example of using the useState hook in a functional component:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
In this example, the Counter component uses the useState hook to manage the count state. The setCount function updates the count value when the user clicks the "Increment" button.
Conclusion:
Congratulations on completing this full tutorial on React JS! You’ve learned the basics of React components, props, state, events, conditional rendering, and hooks. With this knowledge, you can start building dynamic and interactive web applications using React.
Remember that React is a powerful tool that offers a lot of flexibility and scalability for building modern web applications. Practice coding, experiment with different concepts, and continue learning to improve your React skills. Happy coding!
good
plz redux k liye ek single vedio upload kr dijiye bo smjh nhi aaya
nice vedio mera jo confusion bale concept the sb clear ho gye
Thank You
bhai code ka access nhi diya aapne
One of the Best react one – shot tutorial available in youtube . Thanks Bruh..!! 🙂
bhai can i use vite?
great tutorial thank you so much
🫀
2:57 ye konsa pen✒ tool hai kisi ko pata hai?
Sir mujhe react js aur next js me complex ecommerce Web application create karna aata hai
Mai frontend aur backend dono me work kar sakta hu Typescript ka use karke
Frontend –
React, Next js redux-toolkit, Context Api, html, css
Backend –
Node js, express js, jwt, bcrypt, mongodb, cloudinary, redis,
Aur thoda thoda websocket ka Knowledge hai
Par mere paas koi job ya project nhi hai kya karu samajh me nhi aa raha hai
Bhai code kahan hai.. Notes b de dete to accha hota
withihn 5 hours completed this course
Bhai yar love you hu gya akheer kr di ap ny smjhany wali
React m project tu bnaya nai bhai ap ny kb bnay gy but ye video ap ki bht bht bht bht he kamal ki sb clear a to z
21.0 —- props for reuse ability 😅waa bhii waa , lay Ya sunany ky bad , components ham mar jain kya?😂😂
best tutorial for beginners. Also please make separate video for react hooks. You explain very well and I understand it quickly so please make video on reack hooks as well
sir, aap konas app use karte screen par likhne ke liye
Bye bye 😂
flutter series ho jaye??
Please share the code🙏🏼