DEV Community

Cover image for All of Reacts concepts- one article
Albert Jokelin
Albert Jokelin

Posted on

All of Reacts concepts- one article

React, developed by Facebook, has transformed web development with its component-based architecture and efficient rendering. However, mastering React requires a solid grasp of its core concepts.

In this comprehensive article, we'll explore all the essential aspects of React. Whether you're a beginner or an experienced developer, this guide covers everything from components and state management to hooks and lifecycle methods.

By the end, you'll have a thorough understanding of React's ecosystem and be ready to build sophisticated, maintainable applications.

Components

A function that returns markup (JSX). Kind of like legos, basic building blocks of a react app.

function Profile() { return ( <img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" /> ); } export default function Gallery() { return ( <section> <h1>Amazing scientists</h1> <Profile /> <Profile /> <Profile /> </section> ); } 
Enter fullscreen mode Exit fullscreen mode

Curly braces

Allows react to be dynamic by allowing values to pass through it.

 <img className="avatar" src={avatar} alt={description} /> 
Enter fullscreen mode Exit fullscreen mode

Fragments

An empty component is usually used as a parent container to return multiple components simultaneously.

<> <ChildComponent /> </> 
Enter fullscreen mode Exit fullscreen mode

Props

The parameters passed through containers. Anything can be a prop including other components (as children, known as composition).

A 'key' in props (similar to the primary key in SQL) is used to identify a component. Usually, it is the current index.

// Writing a function that supports props function Avatar({ person, size }) { return ( <img className="avatar" src={getImageUrl(person)} alt={person.name} width={size} height={size} /> ); } // Using props with the component return ( <Avatar person={{ name: 'Lin Lanying', imageId: '1bX5QH6' }} size={100} /> ); 
Enter fullscreen mode Exit fullscreen mode

Rendering

Works by using a virtual DOM (VDOM). how does it work:

  • State changed? update VDOM to reflect changes
  • 'Diff'ing is performed: Check for changes between DOM and VDOM.
  • 'Reconciliation' with the DOM is performed.

Image courtesy of Telerik.com, Describes rendering in react

Event Handling

Handles different events like onClick(), onChange(), and onSubmit().

Image description

State

A snapshot of the app at any given point. We use special functions like useState and useReducer.

function 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

Controlled Components

Components used by a state to have a more predictable behaviour.

const [value, setValue] = useState('') 
Enter fullscreen mode Exit fullscreen mode

We change the state to change the behaviour.

Hooks

It allows us to hook into features like state within function components. There are 5 types of hooks:

  • State Hooks- useState, useReducer, to manage state
  • Context Hooks- useContext, use data through context
  • Ref Hooks- useRef, to reference HTML
  • Effect Hooks- useEffect, Lets you connect to external systems and APIs.
  • Performance Hooks- useMemo, useCallback, Boosts performance.

Types of hooks in react

Purity

Describes how react components should work (Like the one in Functional Programming). It follows two rules:

  • Only return JSX
  • Don't change stuff that existed before rendering

Strict Mode

A component that detects problems during app development. Here's how to use it:

<StrictMode> <App /> </StrictMode> 
Enter fullscreen mode Exit fullscreen mode

Effects

Code that reaches outside the react application (like an API). Best done using event handlers or useEffect.

function ChatRoom({ roomId }) { const [serverUrl, setServerUrl] = useState('https://localhost:1234'); useEffect(() => { const connection = createConnection(serverUrl, roomId); connection.connect(); return () => { connection.disconnect(); }; }, [serverUrl, roomId]); // ... } 
Enter fullscreen mode Exit fullscreen mode

Refs

Used to reference an element on DOM. Useful for tasks like focusing on an element.

export default function Counter() { let ref = useRef(0); function handleClick() { ref.current = ref.current + 1; alert('You clicked ' + ref.current + ' times!'); } return ( <button onClick={handleClick}> Click me! </button> ); } 
Enter fullscreen mode Exit fullscreen mode

Context

Passing data without sending as props.

function Button() { const theme = useContext(ThemeContext); return <button className={theme} />; } 
Enter fullscreen mode Exit fullscreen mode

Image courtesy of Shamalka Vanod (medium)

Portals

Context for components. Ideal for modals, dropdowns and tooltips.

<div> <p>This child is placed in the parent div.</p> {createPortal( <p>This child is placed in the document body.</p>, document.body )} </div> 
Enter fullscreen mode Exit fullscreen mode

Suspense

Component to wait for something to load/occur. Provides a fallback UX till the other component is fetched/rendered.

<Suspense fallback={<Loading />}> <SomeComponent /> </Suspense> 
Enter fullscreen mode Exit fullscreen mode

Error Boundaries

Component to show a fallback component should that app encounter an error (like a 404 page).

export function MessageContainer({ messagePromise }) { return ( <ErrorBoundary fallback={<p>⚠️Something went wrong</p>}> <Suspense fallback={<p>⌛Downloading message...</p>}> <Message messagePromise={messagePromise} /> </Suspense> </ErrorBoundary> ); } function Message({ messagePromise }) { const content = use(messagePromise); return <p>Here is the message: {content}</p>; } 
Enter fullscreen mode Exit fullscreen mode

References:
React Dev

Top comments (0)