Introduction to React.js

Hello, and welcome to your first journey into the world of React.js! So, what exactly is React.js? Simply put, it's a JavaScript library used to build user interfaces, specifically for single-page applications.

Websites today are not just simple pages with text and images anymore; they are full-blown applications that rerender, interact with the user, and update live without refreshing the page.

With regular HTML and JavaScript, when we want to update the user interface according to user action or other events, we manually manipulate the DOM and update the required elements. However, with React.js, we divide our user interface into separate reusable components, and React.js effectively decides what parts of the user interface need to be updated when the application's state changes. This approach makes the code cleaner, easier to understand and maintain, and more efficient.

Are you excited? We certainly are. Let's get started!

Introduction to JSX

React introduces a syntax that looks like a combination of JavaScript and HTML. This syntax, known as JSX (JavaScript XML), allows developers to build HTML structures directly within their JavaScript code.

Consider this simple example:

let name = 'John Doe';
const element = <h1>Hello, World!</h1>;

In this code, element is not a string or HTML - it's a JSX element. While it looks like HTML, it's actually a JavaScript expression that creates a React element - a plain JavaScript object that describes what should appear on the screen. React uses these elements to build and update the actual DOM efficiently.

Embedding JavaScript Expressions

With JSX, you're not limited to static HTML-like tags, you can embed JavaScript expressions as well:

let a = 10;
let b = 20;
const sumElement = <h1>The sum is: {a + b}</h1>;

In this example, a + b is a JavaScript expression embedded in JSX. When React renders this component, it will replace {a + b} with the result of the expression, displaying 'The sum is: 30' on the screen.

Utilizing JavaScript Objects and JSX Attributes

JSX allows us to specify any HTML attribute as we normally would. However, some HTML attributes clash with reserved JavaScript keywords (like class attribute, which is written as className in JSX) or are kebab-cased, which is not an accepted naming convention in JavaScript.

To handle this, in JSX, we use camelCase property naming, which is consistent with JavaScript object properties. For example, HTML's tabindex becomes tabIndex in JSX and CSS's background-color changes to backgroundColor.

Let's see these in action with an object and a JSX element:

const elementStyle = {
  fontSize: '14px',
  backgroundColor: '#fafafa',
};

const element = (
  <div style={elementStyle} className="myClass">
    <h1>Hello, World!</h1>
    <p>Welcome to our application!</p>
  </div>
);

In the above example, elementStyle is a plain JavaScript object containing two properties: fontSize and backgroundColor. These properties are written in camelCase, per JavaScript convention.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal