DEV Community

Matt Williams for Tech Dev Blog

Posted on • Originally published at techdevblog.io on

Get a Head Start on Your Career with React! A short intro

Get a Head Start on Your Career with React! A short intro

Welcome to the world of React! If you're new to this popular JavaScript library, don't worry - we'll take it step by step and before you know it, you'll be building awesome user interfaces in no time.

So, what is React? Simply put, it's a library for building user interfaces. It's fast, efficient, and easy to use, and it's quickly become one of the most popular choices for front-end developers. With React, you can build reusable components that make it easy to build complex user interfaces.

Now, let's get started with a simple example. First, make sure you have Node.js and npm (the Node.js package manager) installed on your machine. Then, create a new directory for your project and run the following command to create a new React project using the create-react-app tool:

npx create-react-app my-project
Enter fullscreen mode Exit fullscreen mode

This will create a new directory called my-project with all the files you need to get started with React. Go ahead and navigate into the new directory:

cd my-project
Enter fullscreen mode Exit fullscreen mode

Next, let's start the development server by running the following command:

npm start
Enter fullscreen mode Exit fullscreen mode

This will start the development server and open a new browser window with your React app. You should see a page with the text "Welcome to React" and a logo.

So, how does all this work? Well, the main file you'll be working with is called index.js. This is the entry point for your React app, and it's where you'll write the code to render your user interface.

Here's a simple example of what your index.js file might look like:

import React from 'react';
import ReactDOM from 'react-dom';

const App = () => {
  return (
    <div>
      <h1>Hello, world!</h1>
      <p>Welcome to my first React app!</p>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById('root'));
Enter fullscreen mode Exit fullscreen mode

In this example, we're using a function called App to return a JSX element. JSX is a syntax extension for JavaScript that lets you write HTML-like code in your JavaScript files. It's a powerful tool that makes it easy to build complex user interfaces.

The App function is called a "component", and it's a key concept in React. A component is a piece of code that represents a part of a user interface. You can use components to break down your user interface into smaller, reusable pieces.

In the example above, we're using the ReactDOM.render function to render our App component to the page. The document.getElementById('root') part refers to an element in the HTML file called index.html, which looks like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.
      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.
      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.
      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

Enter fullscreen mode Exit fullscreen mode

Now that you have a basic understanding of how React works, let's continue with some more advanced concepts.

One of the key features of React is its ability to manage state. State is a way to store data in your component that can change over time. For example, you might have a component that displays a list of items, and the state of that component might be the list of items itself.

To manage state in a React component, you can use a special hook called useState. Here's an example of how you might use it:

import React, { useState } from 'react';

const Example = () => {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the useState hook to create a new state variable called count. We're also using a button element with an onClick event handler to update the value of count when the button is clicked.

You can also use state to store data that comes from external sources, such as an API. To do this, you can use the useEffect hook, which lets you perform side effects in a function component. Here's an example of how you might use it:

import React, { useState, useEffect } from 'react';

const Example = () => {
  // Declare a new state variable, which we'll call "data"
  const [data, setData] = useState(null);

  // Fetch data from an API when the component mounts
  useEffect(() => {
    const fetchData = async () => {
      const response = await fetch('https://api.example.com/data');
      const data = await response.json();
      setData(data);
    };
    fetchData();
  }, []);

  return
  (
  <div>
    {data && <p>{data.message}</p>}
  </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the useEffect hook to fetch data from an API when the component mounts. The empty array at the end of the useEffect function tells React to only run the effect once, when the component mounts.

There are many other features of React that we haven't covered in this tutorial, such as props, context, and the new concurrency model with the Suspense and lazy APIs. But with the basics under your belt, you should now be able to start building your own React applications.

So go ahead and give it a try! With React, you'll be able to build fast, efficient, and easy-to-use user interfaces that will make your users happy. Happy coding!

Top comments (0)