{"id":3876,"date":"2023-11-04T23:13:54","date_gmt":"2023-11-04T23:13:54","guid":{"rendered":"http:\/\/localhost:10003\/introduction-to-react\/"},"modified":"2023-11-05T05:48:29","modified_gmt":"2023-11-05T05:48:29","slug":"introduction-to-react","status":"publish","type":"post","link":"http:\/\/localhost:10003\/introduction-to-react\/","title":{"rendered":"Introduction to React"},"content":{"rendered":"

React is a JavaScript library for building user interfaces. It allows developers to create reusable UI components that can be used to build rich and interactive web applications. React was developed by Facebook and is used by many big companies such as Netflix, Instagram, and Dropbox.<\/p>\n

In this tutorial, we will cover the basics of React and show you how to get started building your own React applications.<\/p>\n

Getting started with React<\/h2>\n

Before we dive into building our first React application, let’s do a quick overview of what we need to set up.<\/p>\n

Installation<\/h3>\n

First, we need to install Node.js on our computer. Node.js is a JavaScript runtime that allows us to run JavaScript code outside of the browser. It is required to use React and many other JavaScript libraries.<\/p>\n

You can download the latest version of Node.js from the official website:<\/p>\n

https:\/\/nodejs.org\/en\/download\/\n<\/code><\/pre>\n

Once Node.js is installed, we can use the npm<\/code> command to install React:<\/p>\n

npm install react\n<\/code><\/pre>\n

We will also need a package manager such as npm<\/code> or yarn<\/code> to manage our project dependencies. Here, we will use npm<\/code>. You can install npm<\/code> by running the following command in your terminal:<\/p>\n

npm install npm@latest -g\n<\/code><\/pre>\n

Create a React project<\/h3>\n

Once we have npm<\/code> installed, we can create a new React project by running the following command:<\/p>\n

npx create-react-app my-app\n<\/code><\/pre>\n

This will create a new React project called my-app<\/code> in our current directory. It will also automatically install all required dependencies and set up a basic project structure.<\/p>\n

Project structure<\/h3>\n

When we open our project in a code editor, we will see the following folder structure:<\/p>\n

my-app\/\n\u251c\u2500\u2500 README.md\n\u251c\u2500\u2500 node_modules\/\n\u251c\u2500\u2500 package.json\n\u251c\u2500\u2500 public\/\n\u2502   \u251c\u2500\u2500 favicon.ico\n\u2502   \u251c\u2500\u2500 index.html\n\u2502   \u2514\u2500\u2500 manifest.json\n\u2514\u2500\u2500 src\/\n    \u251c\u2500\u2500 App.css\n    \u251c\u2500\u2500 App.js\n    \u251c\u2500\u2500 App.test.js\n    \u251c\u2500\u2500 index.css\n    \u251c\u2500\u2500 index.js\n    \u2514\u2500\u2500 logo.svg\n<\/code><\/pre>\n

public\/index.html<\/code>: This is the main HTML file for our project. It contains the root div<\/code> element where we will render our React components.<\/p>\n

src\/index.js<\/code>: This is the entry point for our React application. It sets up the React app and renders our main App<\/code> component.<\/p>\n

src\/App.js<\/code>: This is where we will create our main App<\/code> component. We will build our UI by composing multiple other components.<\/p>\n

Building a Simple Counter Component<\/h2>\n

Now that we have our project set up and we understand the file structure, let’s create our first React component.<\/p>\n

In this section, we will create a simple counter component that increments its value every time we click on a button.<\/p>\n

Step 1: Update App.js<\/h3>\n

In our src\/App.js<\/code> file, we will replace the code with the following:<\/p>\n

import React, { useState } from 'react';\nimport '.\/App.css';\n\nfunction App() {\n  const [count, setCount] = useState(0);\n\n  const increment = () => {\n    setCount(count + 1);\n  };\n\n  return (\n    <div className=\"App\">\n      <h1>Counter: {count}<\/h1>\n      <button onClick={increment}>Increment<\/button>\n    <\/div>\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n

Let’s break down the code:<\/p>\n

First, we import React<\/code> and useState<\/code> from the react<\/code> library. useState<\/code> is a hook that allows us to add state to our functional components.<\/p>\n

import React, { useState } from 'react';\n<\/code><\/pre>\n

Next, we define our App<\/code> component. It returns a div<\/code> element containing a header with the current count and a button to increment the count.<\/p>\n

function App() {\n  const [count, setCount] = useState(0);\n\n  const increment = () => {\n    setCount(count + 1);\n  };\n\n  return (\n    <div className=\"App\">\n      <h1>Counter: {count}<\/h1>\n      <button onClick={increment}>Increment<\/button>\n    <\/div>\n  );\n}\n<\/code><\/pre>\n

Inside our App<\/code> component, we define a state variable called count<\/code> using the useState<\/code> hook. The initial value of count<\/code> is 0<\/code>.<\/p>\n

const [count, setCount] = useState(0);\n<\/code><\/pre>\n

We also define a function called increment<\/code> that updates the count<\/code> state variable whenever the button is clicked.<\/p>\n

const increment = () => {\n  setCount(count + 1);\n};\n<\/code><\/pre>\n

Finally, we render the div<\/code> element containing our header and button, passing the count<\/code> and increment<\/code> variables as props.<\/p>\n

return (\n  <div className=\"App\">\n    <h1>Counter: {count}<\/h1>\n    <button onClick={increment}>Increment<\/button>\n  <\/div>\n);\n<\/code><\/pre>\n

Step 2: Start the application<\/h3>\n

To start our application, we need to run the following command in our terminal:<\/p>\n

npm start\n<\/code><\/pre>\n

This will start a development server and launch our application in the browser. If everything is set up correctly, we should see our counter component rendered on the screen.<\/p>\n

Step 3: Test the component<\/h3>\n

To test our component, we can click on the increment button and verify that the count is increasing. We can also inspect the component in the browser dev tools and see how the state is changing.<\/p>\n

Conclusion<\/h2>\n

In this tutorial, we learned the basics of React and how to build a simple counter component. We covered topics such as installation, project structure, component creation, and state management.<\/p>\n

React is a powerful tool for building modern web applications. It allows developers to create reusable UI components that can be easily composed to build complex user interfaces.<\/p>\n

We hope this tutorial has given you a good introduction to React and inspired you to explore its capabilities further. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"

React is a JavaScript library for building user interfaces. It allows developers to create reusable UI components that can be used to build rich and interactive web applications. React was developed by Facebook and is used by many big companies such as Netflix, Instagram, and Dropbox. In this tutorial, we Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[50,46,45,47,48,49,44],"yoast_head":"\nIntroduction to React - Pantherax Blogs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/localhost:10003\/introduction-to-react\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to React\" \/>\n<meta property=\"og:description\" content=\"React is a JavaScript library for building user interfaces. It allows developers to create reusable UI components that can be used to build rich and interactive web applications. React was developed by Facebook and is used by many big companies such as Netflix, Instagram, and Dropbox. In this tutorial, we Continue Reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/localhost:10003\/introduction-to-react\/\" \/>\n<meta property=\"og:site_name\" content=\"Pantherax Blogs\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T23:13:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-05T05:48:29+00:00\" \/>\n<meta name=\"author\" content=\"Panther\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Panther\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t \"@context\": \"https:\/\/schema.org\",\n\t \"@graph\": [\n\t {\n\t \"@type\": \"Article\",\n\t \"@id\": \"http:\/\/localhost:10003\/introduction-to-react\/#article\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/introduction-to-react\/\"\n\t },\n\t \"author\": {\n\t \"name\": \"Panther\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\"\n\t },\n\t \"headline\": \"Introduction to React\",\n\t \"datePublished\": \"2023-11-04T23:13:54+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:29+00:00\",\n\t \"mainEntityOfPage\": {\n\t \"@id\": \"http:\/\/localhost:10003\/introduction-to-react\/\"\n\t },\n\t \"wordCount\": 645,\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"keywords\": [\n\t \"\\\"Front-end development\\\"]\",\n\t \"\\\"JavaScript framework\\\"\",\n\t \"\\\"React basics\\\"\",\n\t \"\\\"React components\\\"\",\n\t \"\\\"React syntax\\\"\",\n\t \"\\\"Web development\\\"\",\n\t \"[\\\"Introduction to React\\\"\"\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"WebPage\",\n\t \"@id\": \"http:\/\/localhost:10003\/introduction-to-react\/\",\n\t \"url\": \"http:\/\/localhost:10003\/introduction-to-react\/\",\n\t \"name\": \"Introduction to React - Pantherax Blogs\",\n\t \"isPartOf\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#website\"\n\t },\n\t \"datePublished\": \"2023-11-04T23:13:54+00:00\",\n\t \"dateModified\": \"2023-11-05T05:48:29+00:00\",\n\t \"breadcrumb\": {\n\t \"@id\": \"http:\/\/localhost:10003\/introduction-to-react\/#breadcrumb\"\n\t },\n\t \"inLanguage\": \"en-US\",\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"ReadAction\",\n\t \"target\": [\n\t \"http:\/\/localhost:10003\/introduction-to-react\/\"\n\t ]\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"BreadcrumbList\",\n\t \"@id\": \"http:\/\/localhost:10003\/introduction-to-react\/#breadcrumb\",\n\t \"itemListElement\": [\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 1,\n\t \"name\": \"Home\",\n\t \"item\": \"http:\/\/localhost:10003\/\"\n\t },\n\t {\n\t \"@type\": \"ListItem\",\n\t \"position\": 2,\n\t \"name\": \"Introduction to React\"\n\t }\n\t ]\n\t },\n\t {\n\t \"@type\": \"WebSite\",\n\t \"@id\": \"http:\/\/localhost:10003\/#website\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"description\": \"\",\n\t \"publisher\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\"\n\t },\n\t \"potentialAction\": [\n\t {\n\t \"@type\": \"SearchAction\",\n\t \"target\": {\n\t \"@type\": \"EntryPoint\",\n\t \"urlTemplate\": \"http:\/\/localhost:10003\/?s={search_term_string}\"\n\t },\n\t \"query-input\": \"required name=search_term_string\"\n\t }\n\t ],\n\t \"inLanguage\": \"en-US\"\n\t },\n\t {\n\t \"@type\": \"Organization\",\n\t \"@id\": \"http:\/\/localhost:10003\/#organization\",\n\t \"name\": \"Pantherax Blogs\",\n\t \"url\": \"http:\/\/localhost:10003\/\",\n\t \"logo\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\",\n\t \"url\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"contentUrl\": \"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg\",\n\t \"width\": 1024,\n\t \"height\": 1024,\n\t \"caption\": \"Pantherax Blogs\"\n\t },\n\t \"image\": {\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/logo\/image\/\"\n\t }\n\t },\n\t {\n\t \"@type\": \"Person\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7\",\n\t \"name\": \"Panther\",\n\t \"image\": {\n\t \"@type\": \"ImageObject\",\n\t \"inLanguage\": \"en-US\",\n\t \"@id\": \"http:\/\/localhost:10003\/#\/schema\/person\/image\/\",\n\t \"url\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"contentUrl\": \"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g\",\n\t \"caption\": \"Panther\"\n\t },\n\t \"sameAs\": [\n\t \"http:\/\/localhost:10003\"\n\t ],\n\t \"url\": \"http:\/\/localhost:10003\/author\/pepethefrog\/\"\n\t }\n\t ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Introduction to React - Pantherax Blogs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/localhost:10003\/introduction-to-react\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to React","og_description":"React is a JavaScript library for building user interfaces. It allows developers to create reusable UI components that can be used to build rich and interactive web applications. React was developed by Facebook and is used by many big companies such as Netflix, Instagram, and Dropbox. In this tutorial, we Continue Reading","og_url":"http:\/\/localhost:10003\/introduction-to-react\/","og_site_name":"Pantherax Blogs","article_published_time":"2023-11-04T23:13:54+00:00","article_modified_time":"2023-11-05T05:48:29+00:00","author":"Panther","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Panther","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/localhost:10003\/introduction-to-react\/#article","isPartOf":{"@id":"http:\/\/localhost:10003\/introduction-to-react\/"},"author":{"name":"Panther","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7"},"headline":"Introduction to React","datePublished":"2023-11-04T23:13:54+00:00","dateModified":"2023-11-05T05:48:29+00:00","mainEntityOfPage":{"@id":"http:\/\/localhost:10003\/introduction-to-react\/"},"wordCount":645,"publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"keywords":["\"Front-end development\"]","\"JavaScript framework\"","\"React basics\"","\"React components\"","\"React syntax\"","\"Web development\"","[\"Introduction to React\""],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/localhost:10003\/introduction-to-react\/","url":"http:\/\/localhost:10003\/introduction-to-react\/","name":"Introduction to React - Pantherax Blogs","isPartOf":{"@id":"http:\/\/localhost:10003\/#website"},"datePublished":"2023-11-04T23:13:54+00:00","dateModified":"2023-11-05T05:48:29+00:00","breadcrumb":{"@id":"http:\/\/localhost:10003\/introduction-to-react\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/localhost:10003\/introduction-to-react\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/localhost:10003\/introduction-to-react\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/localhost:10003\/"},{"@type":"ListItem","position":2,"name":"Introduction to React"}]},{"@type":"WebSite","@id":"http:\/\/localhost:10003\/#website","url":"http:\/\/localhost:10003\/","name":"Pantherax Blogs","description":"","publisher":{"@id":"http:\/\/localhost:10003\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/localhost:10003\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/localhost:10003\/#organization","name":"Pantherax Blogs","url":"http:\/\/localhost:10003\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/","url":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","contentUrl":"http:\/\/localhost:10003\/wp-content\/uploads\/2023\/11\/cropped-9e7721cb-2d62-4f72-ab7f-7d1d8db89226.jpeg","width":1024,"height":1024,"caption":"Pantherax Blogs"},"image":{"@id":"http:\/\/localhost:10003\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"http:\/\/localhost:10003\/#\/schema\/person\/b63d816f4964b163e53cbbcffaa0f3d7","name":"Panther","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/localhost:10003\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b8c0eda5a49f8f31ec32d0a0f9d6f838?s=96&d=mm&r=g","caption":"Panther"},"sameAs":["http:\/\/localhost:10003"],"url":"http:\/\/localhost:10003\/author\/pepethefrog\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3876"}],"collection":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/comments?post=3876"}],"version-history":[{"count":1,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3876\/revisions"}],"predecessor-version":[{"id":4652,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/posts\/3876\/revisions\/4652"}],"wp:attachment":[{"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/media?parent=3876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/categories?post=3876"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:10003\/wp-json\/wp\/v2\/tags?post=3876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}