---
id: 085bfd7c-d52a-11ee-bfc7-b34a261d031c
title: |
How to Learn the Hard Parts of React – and Tips to Conquer Them
status: ARCHIVED
tags:
- read-later
- RSS
date_added: 2024-02-26 18:07:53
url_omnivore: |
https://omnivore.app/me/how-to-learn-the-hard-parts-of-react-and-tips-to-conquer-them-18de8db4244
url_original: |
https://www.freecodecamp.org/news/hard-parts-of-react/
---
# How to Learn the Hard Parts of React – and Tips to Conquer Them
## Highlights
The first instinct for many developers when faced with creating dynamic lists is to use the index property as the key. It seems like a convenient solution, as the index provides a unique identifier for each element in the array – but it isn’t the best approach for the following reasons:
* **Non-Persistent**: If the order or number of items changes, React may get confused. For example, if an item is added or removed from the beginning of the list, all the subsequent indices change, causing potential re-rendering issues.
* **Array Mutations**: Operations like sorting or filtering can alter the order of items, breaking the association between the index and the actual item.
* **Performance Concerns**: React relies on keys for efficient updates. Using the index as a key might impact performance when dealing with large lists or frequent updates.
Some of the better alternatives include:
* **Use a Unique ID**: If each item in your array has a unique identifier, such as an `id` property, use that as the key.
[source](https://omnivore.app/me/how-to-learn-the-hard-parts-of-react-and-tips-to-conquer-them-18de8db4244#e912d65e-4c82-41f1-94d4-56c151254c3e)
---
Generate a Unique Key: In cases where items lack a natural unique identifier, consider using a function like `crypto.randomUUID()` to generate a unique key.
[source](https://omnivore.app/me/how-to-learn-the-hard-parts-of-react-and-tips-to-conquer-them-18de8db4244#9074fe2d-d95d-4fc6-af45-be052bb24de5)
---
To ensure your component behaves as expected and follows React's principles, always use the setter function (`setNames`) to update the state.
[source](https://omnivore.app/me/how-to-learn-the-hard-parts-of-react-and-tips-to-conquer-them-18de8db4244#a42adf04-e7ec-4612-a7ea-c179bfce9163)
---
To safeguard your component from unexpected errors, incorporate optional chaining (`?.`) when accessing nested properties in API data.
[source](https://omnivore.app/me/how-to-learn-the-hard-parts-of-react-and-tips-to-conquer-them-18de8db4244#7ef6781d-7b8b-496d-bd2d-2974995be502)
---
* **Put data outside components**: Move things like lists and groups of information outside the main part of a component when possible. This helps avoid extra updates and makes it simpler to handle data without using special functions like `useCallback`.
* **Be careful with `React.memo`**: Using `React.memo` can help your components run better, but it's not always needed. If a component changes a lot with new information, using `React.memo` might not be as helpful. Use it wisely.
* **Create your own custom React hooks**: I also like making my own special tools with custom React hooks. It's a bit advanced, but it helps keep my code neat and organized.
[source](https://omnivore.app/me/how-to-learn-the-hard-parts-of-react-and-tips-to-conquer-them-18de8db4244#eb5d0b4b-a153-4551-a3a4-522a590854b5)
---
## Original

Have you started learning React, only to face bugs that made you contemplate a career in goat herding? Don't worry – we've all been there.
In this guide, you'll join me on a quest through the quirky wonders of React. I'll help you navigate the perplexing moments, ensuring you never have to ask yourself, "What’s up with React?"
Whether you're a seasoned React adventurer or unearthing the mysteries of virtual DOMs, fear not. I'm here to share the tales of my early struggles, demystify the enigmatic bugs, and pave the way for a smoother journey.
### Prerequisites
* Fundamentals of HTML and CSS
* Fundamentals of ES6 JavaScript and React
## ****What We'll Cover:**
1. [Quick Recap of React Fundamentals](#what-we-ll-cover-)
– [Components: The Web Building Blocks](#components-the-web-building-blocks)
– [JSX: Where HTML Meets JavaScript](#jsx-where-html-meets-javascript)
– [State and Props: The Dynamic Duo](#state-and-props-the-dynamic-duo)
2. [The Good, the Bad, and the Challenging Parts of React](#the-good-the-bad-and-the-challenging-parts-of-react)
– [The Good Parts of React](#the-good-parts-of-react)
– [The Bad Parts of React](#the-bad-parts-of-react)
– [The Challenging Parts of React](#the-challenging-parts-of-react)
– [Key Prop Mishaps](#key-prop-mishaps)
– [Mutating States Directly](#mutating-states-directly)
– [Mysterious Bugs with Conditional Rendering](#mysterious-bugs-with-conditional-rendering)
– [Ignoring Dependency Arrays in React Hooks](#ignoring-dependency-arrays-in-react-hooks)
– [Neglecting Optional Chaining for API Data](#neglecting-optional-chaining-for-api-data)
– [Ignoring React Fragments for Grouping JSX Elements](#ignoring-react-fragments-for-grouping-jsx-elements)
3. [Opinionated Approaches to React](#opinionated-approaches-to-react)
4. [Wrapping Up the Quirky Journey with React](#wrapping-up-the-quirky-journey-with-react)
## Quick Recap of React Fundamentals
The React library revolves around 3 building blocks: Components, JSX, and State & Props.
### Components: The Web Building Blocks
Imagine components as the LEGO bricks of your user interface—a single, reusable piece that contributes to the grand structure. They encapsulate functionality, styling, and behavior, making your UI both modular and scalable.
From a simple button to an elaborate sidebar, components are the heart and soul of React development.
### JSX: Where HTML Meets JavaScript
JSX, or JavaScript XML, may seem like an odd fusion of HTML and JavaScript at first, but it’s quite straightforward. It's the secret sauce that makes React's syntax so expressive and dynamic.
With JSX, you write your UI components using a syntax that resembles HTML, but underneath, it's pure JavaScript.
### State and Props: The Dynamic Duo
The dynamic duo of state and props bring React pages to life as they add interactivity to your web applications.
#### State: Granting Memory to Components
State provides memory to components, allowing them to remember past events and alter their behavior over time. It's the key to making your UI responsive and dynamic.
Picture a form that remembers the user's input or a counter that increments with each click. That's the magic of state.
#### Props: Enabling Communication
Props (short for properties) facilitate communication between components. They allow parent components to pass data down to their children, creating a seamless flow of information.
Think of props as messengers, ensuring that each component knows its role and receives the necessary information to perform it.
## The Good, the Bad, and the Puzzling Parts of React
Before we delve into the puzzling aspects of React, it's essential to shine a spotlight on the treasures that make React a true hero in your arsenal.
### The Good Parts of React
#### Virtual DOM and its Advantages
The virtual DOM is a revolutionary concept that gives React its speed and efficiency.
When changes occur in your app, React doesn't immediately update the actual DOM. Instead, it works with a lightweight copy, the Virtual DOM, making minimal, lightning-fast adjustments. This not only optimizes performance but also provides a smoother user experience.
```reasonml
ReactDOM.createRoot(document.getElementById("root")).render(
);
```
This process leverages [React's diffing algorithm](https://legacy.reactjs.org/docs/reconciliation.html) in the Virtual DOM. It identifies the minimal set of changes needed in the actual DOM to reflect the updated state.

Explaining how React updates the UI using the virtual DOM
#### Reusable Components
In React, the guiding principle is reusability. Components, the fundamental building blocks we discussed above, can be crafted and employed across your application. This not only fosters a modular and organized code structure but also frees you from the burden of reinventing the wheel.
```hsp
// Reusable Button Component
const Button = ({ label, onClick }) => (
);
// Usage