Skip to main content

ReactJS Tutorial for Beginners

ReactJS Tutorial for Beginners

ReactJS Tutorial for Beginners

React is a JavaScript library for building user interfaces. It lets you compose complex UIs from small and isolated pieces of code called "components".


We'll start by learning how to use React's "Hello World" example. Then we'll move on to setting up a development environment and creating a simple React component. By the end of this tutorial, you'll be able to create a basic React app and understand how React works.


1. Hello World

React's "Hello World" example is a simple one, but it illustrates the basic usage of React. We create a component called <Welcome /> and render it to the DOM with the ReactDOM.render() method.


import React from 'react';

import ReactDOM from 'react-dom';


const Welcome = () => {

return <h1>Hello, world!</h1>;

};


ReactDOM.render(

<Welcome />,

document.getElementById('root')

);


2. Setting up your Development Environment

Before we can start writing React code, we need to set up a development environment. This will allow us to use a JavaScript transpiler (such as Babel) and a bundler (such as webpack).


We'll use the create-react-app tool to set up our environment. This will give us a simple development server and a basic directory structure.


To install create-react-app, open a terminal and run:


npm install -g create-react-app


Once it's finished installing, we can create our new React app with the create-react-app command:


create-react-app my-app


This will create a new directory called my-app with our React app inside.


To start the development server and open our app in a browser, we can run:


cd my-app

npm start


3. Creating a React Component

Now that we have a basic React app up and running, let's create our first React component.


Components are the building blocks of React apps. A component is a piece of UI that can be reusable throughout your app. In other words, if you have a section of your app that you want to use in multiple places, you can create a component for it.


React components are written in JavaScript, but they use a special syntax called JSX. JSX is a syntax extension for JavaScript that lets you write HTML-like code inside JavaScript.


Don't worry if you've never seen JSX before. It's not required to use React and we won't be using it much in this tutorial. However, it's good to know that it exists and how to use it, as you'll see JSX in many React examples and codebases.


Let's create a new file called src/components/Welcome.js and add the following code:


import React from 'react';


const Welcome = () => {

return (

<div>

<h1>Hello, world!</h1>

<p>I'm a React component</p>

</div>

);

};


export default Welcome;


This defines a new React component called Welcome. The component is a function that returns a JSX element. In this case, it returns a <div> element with a <h1> and a <p> inside.


We then use the export keyword to make the component available for other files to import.


4. Rendering a React Component

Now that we have a component, we need to render it to the DOM.


We can do this with the ReactDOM.render() method. This method takes two arguments: the JSX element to render and the DOM element to render it into.


Let's import our Welcome component into the src/index.js file and render it:


import React from 'react';

import ReactDOM from 'react-dom';

import Welcome from './components/Welcome';


ReactDOM.render(

<Welcome />,

document.getElementById('root')

);


If we restart the development server and open our app in the browser, we should see the component rendered to the page:


Hello, world!

I'm a React component


And that's it! You've now learned the basics of React. In the next tutorial, we'll learn about state and props, which are the two main ways to pass data to React components.

Comments

Popular posts from this blog

What is OOP (objects oriented programming) in C#

 What is OOP? in C# OOP is Object Oriented programming miens contain methods and data in objects it's called objects oriented programming(OOP) OOP Advantages Provides clear visibility and code for the programs easier to maintain, modify and debug Faster development easier and faster to execute create reusable code make your code neat and clean and easy to understand What is Class and objects in C# Class and object are the two main points of OOP (object oriented programming), when fruit is a class then Apple, Guava, Banana,  is object, when the individual objects are created they inherits all variables and method form the class, class is a template for the objects and object is instance of the class If you like this blog so pls share and  Write Comments  about Your experience, Thank You.

Make api in DotNet Core in 10sec | Create universal api for SQL server

Make API in Dot Net Core in 10sec | Create universal API for SQL server Universal API is great concept for app and angular developer need only connect to data base add table name and access table crud operation using API's  If you like this blog so pls share and  Write Comments  about Your experience, Thank You.

How to Optimize Your LinkedIn Profile as a Software Developer: Tips for Success

How to Optimize Your LinkedIn Profile as a Software Developer: Tips for Success To make your LinkedIn profile more attractive and discoverable as a software developer, you should focus on creating a compelling profile that showcases your skills, experience, and potential. Below are some specific strategies: 1. Professional Profile Picture Use a clear, professional photo where you look approachable and confident. A headshot with a neutral background works well. 2. Headline Your headline should be more than just your job title. Make it a powerful value proposition. Consider including: Your current role, tech stack, or specialization. Highlight key skills (e.g., "JavaScript | React | Node.js | Full-Stack Developer"). Optional: Include a personal tagline (e.g., "Building innovative web applications that solve real-world problems"). Example: "Full-Stack Developer | React, Node.js, Python | Passionate About Clean Code & Scalable Systems" 3. Summary (About Se...