Jay Abhani
Senior Web Development Instructor at almaBetter
Master React with this ultimate React Cheat Sheet! Covering hooks, TypeScript, testing, syntax and more, it is perfect for beginners, pros, and interview prep
React is one of the most popular JavaScript libraries for building user interfaces. Whether you're just starting or preparing for an interview, this React cheat sheet covers all the essential concepts you need to know. From basic syntax to advanced hooks and TypeScript integration, this guide has got you covered.
React is a JavaScript library for building fast, interactive user interfaces. It is component-based, declarative, and can manage complex state with ease. Here's why React is so widely adopted:
To create a React app, you can use Create React App or tools like Vite for a faster setup.
Using Create React App:
npx create-react-app my-app cd my-app npm start |
Using Vite:
npm create vite@latest my-app --template react cd my-app npm install npm run dev |
JSX is a syntax extension for JavaScript, allowing you to write HTML-like code inside JavaScript:
const App = () => <h1>Hello, React!</h1>; |
Components are reusable pieces of UI. They can be functional or class-based.
Functional Component:
function Greeting() { return <h1>Hello, World!</h1>; } |
Class Component:
class Greeting extends React.Component { render() { return <h1>Hello, World!</h1>; } } |
Props allow components to receive data:
function Welcome(props) { return <h1>Hello, {props.name}!</h1>; } <Welcome name="React" />; |
State is used to manage dynamic data:
function Counter() { const [count, setCount] = React.useState(0); return ( <div> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); } |
React Hooks let you use state and lifecycle methods in functional components.
Manage state in a functional component:
const [state, setState] = React.useState(initialValue); |
Run side effects in components:
React.useEffect(() => { console.log('Component Mounted'); return () => console.log('Component Unmounted'); }, []); |
Access context values:
const value = React.useContext(MyContext); |
Manage complex state logic:
const [state, dispatch] = React.useReducer(reducer, initialState); |
Optimize performance:
const memoizedValue = React.useMemo(() => computeValue(a, b), [a, b]); const memoizedCallback = React.useCallback(() => doSomething(a), [a]); |
Here's a quick reference for essential React syntax:
Rendering a Component:
ReactDOM.render(<App />, document.getElementById('root')); |
Conditional Rendering:
{isLoggedIn ? <LogoutButton /> : <LoginButton />} |
Lists and Keys:
items.map(item => <li key={item.id}>{item.name}</li>); |
Fragments:
<> <p>Paragraph 1</p> <p>Paragraph 2</p> </> |
TypeScript enhances React development with static typing.
npx create-react-app my-app --template typescript |
type Props = { name: string; age?: number; }; const Welcome: React.FC<Props> = ({ name, age }) => ( <h1> Hello, {name}! {age && `You are ${age} years old.`} </h1> ); |
React Query simplifies data fetching, caching, and updating.
npm install react-query |
Example:
import { useQuery } from 'react-query'; const { data, error, isLoading } = useQuery('todos', fetchTodos); if (isLoading) return 'Loading...'; if (error) return 'Error!'; return <div>{data.map(todo => <p key={todo.id}>{todo.text}</p>)}</div>; |
React Testing Library helps test components effectively.
npm install @testing-library/react |
Example Test:
import { render, screen } from '@testing-library/react'; import App from './App'; test('renders Hello, React!', () => { render(<App />); expect(screen.getByText(/Hello, React!/i)).toBeInTheDocument(); }); |
React Native enables building mobile apps using React.
npx react-native init MyApp |
Core Components:
Example:
import { View, Text, Button } from 'react-native'; export default function App() { return ( <View> <Text>Hello, React Native!</Text> <Button title="Click Me" onPress={() => alert('Hello!')} /> </View> ); } |
Key topics to prepare for React interviews:
Example Interview Question:
What are the differences between useEffect and componentDidMount?
This React cheat sheet provides a comprehensive guide for mastering React, from basic syntax to advanced topics like hooks, testing, and TypeScript. Use it as a quick reference while coding or preparing for an interview. Whether you're a beginner or a seasoned developer, React's ecosystem has tools to make your life easier.
More Cheat Sheets and Top Picks