Sitemap
Elvis Edeh

Elvis Edeh

INTRODUCTION TO REACT

3 min readFeb 11, 2023

--

Press enter or click to view image in full size

React.js is a JavaScript library for building user interfaces. It was developed by Facebook and is now maintained by Facebook and a community of individual developers and companies.

React is known for its declarative approach to programming, which makes it easier to understand and debug code. It uses a virtual DOM (Document Object Model) that helps improve the performance of web applications by limiting the number of updates made to the actual DOM.

In summary.

React.js is a powerful and widely-used library for building modern user interfaces, and its modular design and rich ecosystem make it a popular choice among web developers.

CONDITIONAL RENDERING.

Conditional rendering in React refers to the technique of showing or hiding elements in the user interface based on certain conditions or state.

In React, you can perform conditional rendering using conditional statements like if or switch inside the render method or using ternary operators. You can also use helper components to wrap the elements that need to be conditionally rendered.

Here is an example of using a ternary operator for conditional rendering in React:

javascriptCopy code
render() {
return (
<div>
{this.state.isLoading ? (
<p>Loading...</p>
) : (
<p>Data Loaded</p>
)}
</div>
);
}

In this example, if the isLoading state is true, the text "Loading..." will be displayed, otherwise, the text "Data Loaded" will be displayed.

Get Elvis Edeh’s stories in your inbox

Join Medium for free to get updates from this writer.

Conditional rendering is a powerful feature in React that helps you create dynamic and flexible user interfaces.

KEYS & HOOKS.

React “keys” are used to uniquely identify elements in an array of elements that are being rendered in the component. The key helps React to identify which items have changed, been added, or been removed. This optimization helps to improve the overall performance of the component.

Here is an example of using keys in React:

javascriptCopy code
render() {
const list = [{id: 1, text: 'Item 1'}, {id: 2, text: 'Item 2'}];
return (
<ul>
{list.map(item => (
<li key={item.id}>{item.text}</li>
))}
</ul>
);
}

In this example, each li element in the list is given a unique key prop with the value of item.id. This ensures that each item in the list has a unique identifier.

Hooks are a feature in React that allow you to add state and other React features to functional components. They were introduced in React 16.8 and have since become the preferred way to handle state and other React features in functional components.

Here is an example of using the useState hook in React:

javascriptCopy code
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}

In this example, the useState hook is used to add a state to the component. The useState hook returns an array with two values: the current state value, and a function that can be used to update the state value. The state value, count, is initialized to 0. When the button is clicked, the setCount function is called, which updates the count state value.

Hope this was helpful to you. If it was, then don’t forget to clap and leave a comment.

--

--