Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

Preventing Rendering in React

Learn how to prevent a React component from rendering its content by returning null, using practical examples and JSX.

1. What Is Preventing Rendering?

In React, preventing rendering means deciding that a component should not display any content in the user interface. A component can return null when its content should not be displayed.

React components can use conditions to decide whether to return JSX or null. This is useful for hiding notifications, optional messages, and other UI elements.

Remember: Returning null from a React component means that the component renders no UI.

2. Syntax

The basic syntax is:

if (!condition) {
  return null;
}
Notification.jsx JSX
function Notification() {
    const isVisible = false;

    if (!isVisible) {
        return null;
    }

    return <p>You have a new notification!</p>;
}

export default Notification;

Code Explanation

  • isVisible determines whether the notification should be displayed.
  • !isVisible checks whether the value is false.
  • When the condition is true, return null prevents the notification from appearing.
  • When isVisible is true, the component returns the paragraph element.

Expected Output

Browser Output

No notification is displayed.

3. How Does Preventing Rendering Work?

React checks the condition and returns either null or JSX depending on the required output.

React component starts
↓
Check the condition
Hide content
↓
Return null
↓
No UI is displayed
Display content
↓
Return JSX
↓
UI is displayed

4. Example: Hide a Welcome Message

Suppose a welcome message should appear only for registered students. If the student is not registered, the component returns null.

WelcomeMessage.jsx JSX
function WelcomeMessage() {
    const isRegistered = false;

    if (!isRegistered) {
        return null;
    }

    return (
        <div>
            <h2>Welcome to CIIT!</h2>
            <p>Start learning React today.</p>
        </div>
    );
}

export default WelcomeMessage;

Expected Output

Browser Output

The welcome message is not displayed.

Try it: Change isRegistered to true. The welcome heading and paragraph will be rendered.

5. Prevent Rendering with Props

A parent component can pass a prop to control whether a child component displays its content.

Message.jsx JSX
function Message({ show }) {
    if (!show) {
        return null;
    }

    return (
        <p>Your form was submitted successfully!</p>
    );
}

function App() {
    return (
        <div>
            <h2>Form Status</h2>
            <Message show={false} />
        </div>
    );
}

export default App;

Code Explanation

  • The Message component receives the show prop.
  • When show is false, the component returns null.
  • The parent passes false, so the message is hidden.
  • Passing true allows the message to appear.

Expected Output

Browser Output

Form Status

The success message is not displayed.

6. Returning null vs Conditional Rendering

React provides different approaches to decide which content appears in the user interface.

Returning null Logical AND Operator Ternary Operator
Returns no UI from the component. Displays content when a condition is truthy. Chooses between two expressions.
Useful when the whole component should display nothing. Useful for optional content inside JSX. Useful when true and false cases need different output.
return null; show && <p>Hello</p> show ? <p>Hello</p> : null

7. Common Mistakes to Avoid

1. Using an empty string instead of null

Not recommended:

function Message() {
    return "";
}

An empty string is not the clearest way to express that a component should render no UI. Use null when that is the intended behavior.

Recommended:

function Message() {
    return null;
}

2. Forgetting to return null

function Notification({ show }) {
    if (!show) {
        return;
    }

    return <p>New notification!</p>;
}

The early return above returns undefined. For a component intended to render nothing, explicitly use return null;.

8. Practice Tasks

Try these exercises using React components:

  1. Create a LoginMessage component that returns null when the user is logged out.
  2. Create an ErrorMessage component that displays an error only when the hasError prop is true.
  3. Create a CourseNotice component that returns null when no courses are available.
  4. Create a SuccessMessage component that accepts a show prop.
  5. Create a parent component that passes true and false values to a child component to control its rendering.
Practice Tip: Write the condition before the JSX return statement when you want to prevent the entire component from rendering.

Summary

Preventing rendering in React allows a component to return null when no user interface should be displayed. A condition can determine whether the component returns JSX or null. This technique is useful for hiding notifications, messages, and optional UI elements. Components can also use props to control their rendering. Returning null means that the component renders no UI for that return.