? All-In-One React - Js Guide For Students & Developers?
? All-In-One React - Js Guide For Students & Developers?
JS
CheatSheet
TABLE OF CONTENTS
1. Introduction to React
What is React?
History and Features
Virtual DOM Explained
React vs Vanilla JS vs Other Frameworks
2. Environment Setup
Installing Node.js and npm
Create React App (CRA)
Project Structure
Using Vite for Faster Setup
3. JSX Basics
What is JSX?
JSX Syntax Rules
Embedding Expressions
JSX Best Practices
4. Components
Functional Components
Class Components
Component Naming and Structure
Props in Components
Props Destructuring
Children Props
5. State Management
useState Hook
setState in Class Components
Lifting State Up
Passing State as Props
6. Event Handling
Handling Events in React
Event Binding
Synthetic Events
Prevent Default and Event Objects
TABLE OF CONTENTS
7. Conditional Rendering
if/else Statements
Ternary Operators
Logical && Operator
Switch Statements
9. Forms in React
Controlled vs Uncontrolled Components
Handling Inputs (text, checkbox, radio, select)
Form Submission
Form Validation Basics
20. Portals
Creating Portals
Use Cases (Modals, Tooltips)
25. Deployment
Building for Production
Hosting on Vercel, Netlify, Firebase
GitHub Pages Deployment
Environment Variables
In simple words:
React helps you build websites that are fast, easy to manage, and don’t
reload every time something changes.
Key Features:
Component-based: You build UI in small pieces called components.
Reusable: One component can be used in many places.
Virtual DOM: It updates only the changed part of the page instead of the
whole page.
Fast and Efficient: Because of virtual DOM and smart updates.
DOM Updates Virtual DOM Full page reload Virtual DOM or optimized updates
Steps:
1.Go to https://nodejs.org
2.Download the LTS version (Recommended for most users)
3.Install it
If you see the versions, Node.js and npm are installed correctly.
Used like:
jsx
Example:
jsx
6. EVENT HANDLING
6.4 Prevent Default and Event Objects
If you want to stop the default action (like refreshing the page on form
submit), use event.preventDefault().
Example:
jsx
7. CONDITIONAL RENDERING
7.1 if/else Statements
You can use normal if/else logic before the return.
jsx
Checkbox:
jsx
Radio:
jsx
Select dropdown:
jsx
jsx
It runs once after the component loads and gets the data.
11. OTHER REACT HOOKS
11.1 useRef
useRef is used to store a value that doesn’t need to re-render the
component.
It’s also used to directly access a DOM element (like an input box).
jsx
11.2 useContext
It helps to share values like user info or theme between components
without props.
jsx
11. OTHER REACT HOOKS
11.3 useReducer
Like useState, but better when state is complex (like a counter with
actions).
jsx
11.4 useMemo
Stops a function from running again if the inputs (dependencies) didn’t
change.
jsx
11.5 useCallback
Stops a function from re-creating again and again.
jsx
jsx
14.4 Zustand
Lightweight library, less setup.
jsx
14.5 Recoil
Works with atoms (like small pieces of state).
jsx
jsx
jsx
16. REACT LIFECYCLE (CLASS COMPONENTS)
In classcomponents, lifecycle methodsletyouruncodeatspecific times
in a component’s life (mount, update, unmount).
16.1 constructor()
Runs first, when the component is created.
Used to initialize state and bind methods.
jsx
16.2 render()
Required method.
Returns JSX to display on the screen.
jsx
16.3 componentDidMount()
Called after component is added to the DOM.
Good for API calls or timers.
jsx
16.4 componentDidUpdate()
Runs when props or state change.
Good for updating based on changes.
jsx
16. REACT LIFECYCLE (CLASS COMPONENTS)
16.5 componentWillUnmount()
Called before component is removed.
Use it to clear timers or remove listeners
jsx
17. ERROR HANDLING
17.1 Error Boundaries
A special class component that catches JavaScript errors in children.
jsx
jsx
jsx
18. CODE SPLITTING AND LAZY LOADING
18.1 React.lazy() and Suspense
React.lazy() loads components only when needed.
jsx
jsx
jsx
App. jsx
jsx
20. PORTALS IN REACT
20.4 Use Cases of Portals
Modals/Popups
When you want a window to appear on top of everything else.
Tooltips
Small hint boxes that appear on hover.
Dropdown Menus
If a dropdown needs to overflow a parent with overflow: hidden.
Example:
jsx
What it does:
Warns about unsafe code.
Helps with future-proofing your app.
Does not show anything to the user.
Works only in development (not in production).
22. HIGHER ORDER COMPONENTS (HOC)
22.1 What is an HOC?
An HOC is a function that takes a component and returns a new
component.
jsx
Pros
Reuse logic easily
Cleaner components
Cons
Can become complex to debug
Not as popular now (Hooks are preferred)
23. CUSTOM HOOKS
23.1 When to Create Custom Hooks
When you find yourself copying the same useEffect, useState, or logic in
multiple places — create a custom hook.
23.2 Example: useLocalStorage
jsx
Usage:
jsx
Usage:
jsx
Add to package.json:
json
Then run:
bash
25. DEPLOYMENT
25.6 Environment Variables
You can use environment variables for API keys or settings.
Create a .env file:
env
Use in code:
jsx
26. REACT WITH APIS
26.1 Fetching with fetch and axios
➤ Using fetch:
jsx
➤ Using axios:
jsx
27.4 Memoization
Use useMemo or useCallback for expensive calculations or stable function
references.
jsx
tsx
Ant Design
Enterprise UI with lots of components
bash
Chakra UI
Easy-to-use, accessible components
bash
Tailwind CSS
Utility-first CSS framework
bash
ShadCN/UI
Beautiful components built with Tailwind, Radix UI
bash
30. COMMON MISTAKES & PRACTICE TASKS (BONUS SECTION)
31.1 Common Beginner Mistakes in React
Mutating state directly React won't detect changes and Always use setState() or
re-render setSomething()
React has trouble tracking items Use unique key props in
Missing key in lists .map()
Include the correct
Incorrect useEffect usage Missing dependency array can dependencies or use [] for
run-once
cause infinite loops
Always call hooks at the top
level of the component
Calling hooks conditionally Breaks the rules of hooks
Use context or state where
appropriate
Overusing props instead of state Can lead to prop drilling