Getting Started with React.js: The Basics

Kevin Diesenberg
2 min readMay 18, 2023

--

React.js is a powerful JavaScript library for building user interfaces, primarily for single-page applications. It’s used for handling the view layer in web and mobile apps. React.js allows you to design simple views for each state in your application, and it will efficiently update and render the right components when your data changes.

What is React.js?

Facebook developed React.js which Facebook and a community of individual developers and companies now maintain. It is known for its flexibility, performance, and intuitive programming model.

import React from 'react';

class HelloWorld extends React.Component {
render() {
return <h1>Hello, World!</h1>;
}
}

export default HelloWorld;

This simple component renders a heading saying “Hello, World!”.

React Components

The core concept in React.js is components. A React application is essentially a tree of nested components. Each component is a reusable piece of code that returns a React element to be rendered to the DOM.

There are two types of components in React.js, Functional components, and Class components.

Functional Component

Functional components are just JavaScript functions. They accept an input (called ‘props’) and return what should be rendered.

import React from 'react';

const HelloWorld = (props) => {
return <h1>Hello, {props.name}!</h1>;
}

export default HelloWorld;

Class Component

Class components involve ES6 classes. They should extend React.Component and implement a render method.

import React from 'react';

class HelloWorld extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

export default HelloWorld;

JSX

JSX is a syntax extension for JavaScript which is used with React to describe what the UI should look like. It might remind you of a template language, but it comes with the full power of JavaScript.

const element = <h1>Hello, World!</h1>;

State and Props

State is a data structure that starts with a default value when a Component mounts. It may be mutated across time (mostly due to user events).

Props (short for properties) are a Component’s configuration. Props are received from above in the tree and are immutable as far as the Component receiving them is concerned.

class HelloUser extends React.Component {
constructor(props) {
super(props);
this.state = { name: 'John' };
}

render() {
return <h1>Hello, {this.state.name}!</h1>;
}
}

Why Use React.js?

React.js is powerful and provides several benefits:

  • Easy creation of dynamic applications
  • Improved performance due to virtual DOM
  • Reusable components
  • Unidirectional data flow
  • A small learning curve, especially if you are familiar with JavaScript

React.js is a fantastic tool for building flexible, high-performance applications. With its focus on a modular architecture, it promotes code reuse and makes it easier to develop and maintain complex applications.

In the next posts, we’ll dig deeper into more advanced concepts in React.js. Until then, happy coding!

--

--

Kevin Diesenberg
Kevin Diesenberg

Written by Kevin Diesenberg

Software Engineer | JavaScript | React | Ruby | Rails | SQL | Skydiver | Outdoor Enthusiast | Paramotor Pilot | Cyclist | Edible Mushroom Hunter

Responses (1)