Learnitweb

Category: React tutorial

  • Call hook on a condition like button click or input value

    In React, hooks must not be called conditionally or inside event handlers directly — they must always be called at the top level of a functional component or inside a custom hook. However, you can use state to control when the effect of a hook should happen. Here’s how to trigger logic in a hook…

  • How to apply validation on props in React?

    In React, prop validation is used to ensure that components receive props of the expected type and shape. This helps catch bugs early by throwing warnings in development if props are missing or invalid. React provides a built-in library called prop-types for validating props. Step-by-Step: How to Apply Validation on Props Install prop-types Package If…

  • Children prop in React

    In React, the children prop is a special built-in prop automatically passed to every component. It represents the content between the opening and closing tags of a component. It enables component composition, allowing you to build flexible and reusable UI structures. Basic Usage Example: Output: Explanation: Why Use children? 1. Component Composition 2. Reduces Prop…

  • What is a Higher-Order Component (HOC) in React?

    A Higher-Order Component (HOC) is a function that takes a component and returns a new enhanced component. This is a design pattern in React for reusing component logic. Think of it as a “component factory” — a function that takes a component, enhances it, and returns a new component with added features or behavior. Why…

  • How to listen to state changes in react?

    In React, to listen to state changes in functional components, the recommended way is to use the useEffect hook. React does not provide a direct observer-like system for watching state changes — instead, you can react to them with side effects. 1. Use useEffect to Listen to State Changes How it works: 2. Listen to…

  • Can you force a component re-render without calling setstate?

    In functional components, React re-renders the component only when state or props change. However, if you want to force a re-render without changing meaningful state, there are a few techniques you can use. 1. Use a Dummy useState Value This is the most common and recommended way to force a re-render: Explanation: 2. Use useReducer…

  • What is the difference between declarative and imperative in React?

    In React, the terms declarative and imperative describe two different styles of programming, particularly how you express logic and UI behavior. Overview Style Description React Style? Imperative You explicitly tell the program how to do something step by step. Not preferred in React Declarative You describe what you want the UI to look like, and…

  • What are synthetic events in React?

    A Synthetic Event is an object that wraps the native browser event, following the W3C spec to ensure consistency. It behaves like the native event (has the same interface) but is managed internally by React. When you write: The onClick receives a SyntheticEvent, not the raw DOM MouseEvent. Why Use Synthetic Events? React uses synthetic…

  • Controlled vs Uncontrolled Components in React

    React offers two primary ways to handle form elements—Controlled Components and Uncontrolled Components. Understanding the internal behavior and ideal use cases of each is essential for managing user input efficiently in React applications. 1. What is a Controlled Component? A controlled component is one where the form element’s value is bound to React state. The…

  • react-error-boundary component

    Getting started Reusable React error boundary component. Supports all React renderers (including React DOM and React Native). Wrap an ErrorBoundary component around other React components to “catch” errors and render a fallback UI. The component supports several ways to render a fallback. ErrorBoundary is a client component. You can only pass props to it that are serializeable or use it in files…