React Cheat Sheet
React Cheat Sheet
Page 1/2
1. Basics of React
jsx
Copy
function MyComponent() {
return <div>Hello, World!</div>;
}
• JSX: JavaScript XML (HTML-like syntax in JavaScript).
jsx
Copy
Copy
Copy
function Greet(props) {
return <h1>Hello, {props.name}</h1>;
}
<Greet name="John" />;
• State: Manage dynamic data in components.
jsx
Copy
Copy
function handleClick() {
alert('Button clicked!');
}
<button onClick={handleClick}>Click Me</button>;
• Mounting:
o constructor()
o render()
o componentDidMount()
• Updating:
o shouldComponentUpdate()
o render()
o componentDidUpdate()
• Unmounting:
o componentWillUnmount()
jsx
Copy
Copy
useEffect(() => {
// Code to run on mount/update
return () => {
// Cleanup (e.g., remove event listeners)
};
}, [dependencies]);
• useContext: Access context values.
jsx
Copy
const value = useContext(MyContext);
• useRef: Reference DOM elements or persist values.
jsx
Copy
Page 2/2
4. Conditional Rendering
• Ternary Operator:
jsx
Copy
Copy
• Rendering Lists:
jsx
Copy
Copy
<li key={item.id}>{item.name}</li>
6. Forms
• Controlled Components:
jsx
Copy
function MyForm() {
const [value, setValue] = useState('');
return (
<form>
<input
type="text"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</form>
);
}
• Install:
bash
Copy
Copy
8. Context API
• Create Context:
jsx
Copy
Copy
Copy
• Install:
bash
Copy
Copy
Copy