Catalog / React Cheatsheet
React Cheatsheet
A comprehensive cheat sheet covering React fundamentals, JSX syntax, component lifecycle, hooks, and common patterns, designed to provide a quick reference for React developers.
React Fundamentals
JSX Syntax
JSX (JavaScript XML): Allows writing HTML-like syntax in JavaScript. Example:
|
Embedding Expressions: Use curly braces Example:
|
JSX Attributes: Define HTML attributes using JSX syntax. Example:
|
Conditional Rendering: Use ternary operators or Example:
|
Rendering Lists: Use Example:
|
Fragments: Use Example:
|
Components
Function Components: Simple components that accept props and return JSX. Example:
|
Class Components: Components that use ES6 classes, extend Example:
|
Props: Data passed from parent to child components. Props are immutable from the component’s perspective. Example:
|
State: Internal data of a component that can change over time. Changes to state trigger re-rendering. Example:
|
Handling Events: React events are named using camelCase and pass a function as the event handler. Example:
|
Component Composition: Building complex UIs by composing simpler components. Example:
|
Lifecycle Methods & Hooks
Lifecycle Methods (Class Components)
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Hooks (Function Components)
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Advanced Concepts
Context API
Creating a Context:
|
Providing a Context:
|
Consuming a Context:
Or using
|
Higher-Order Components (HOCs)
Definition: A function that takes a component and returns a new, enhanced component. Example:
|
Render Props
Definition: A technique for sharing code between React components using a prop whose value is a function. Example:
|
Error Boundaries
Definition: React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI. Example:
|
Common Patterns and Best Practices
Controlled vs. Uncontrolled Components
Controlled Component: Example:
|
Uncontrolled Component: Example:
|
Code Splitting
Definition: Breaking down the application into smaller chunks, loading only the necessary code for a particular route or feature. Achieved using Example:
|
Memoization
Definition: Optimizing performance by preventing unnecessary re-renders. Use Example:
|