Helping great web books reach the developers who need them most.
Get the full book here - www.amazon.com/Learn-React-...
Get the full book here - www.amazon.com/Learn-React-...
Add a close button using useState:
const [visible, setVisible] = useState(true);
if (!visible) return null;
...
{closable && <button onClick={() => setVisible(false)}>❌</button>}
Now it's dismissible!
Add a close button using useState:
const [visible, setVisible] = useState(true);
if (!visible) return null;
...
{closable && <button onClick={() => setVisible(false)}>❌</button>}
Now it's dismissible!
<span>{heading}</span>
</div>
<div>{children}</div>
</div>
);
}
Use it like this:
<Alert type="information" heading="Success">Everything is really good!</Alert>
Now your component adapts to different contexts
<span>{heading}</span>
</div>
<div>{children}</div>
</div>
);
}
Use it like this:
<Alert type="information" heading="Success">Everything is really good!</Alert>
Now your component adapts to different contexts
Pass type, heading, and children:
xport function Alert({ type = 'information', heading, children }) {
return (
<div>
<div>
<span role="img" aria-label={type === 'warning' ? 'Warning' : 'Information'}>
{type === 'warning' ? '⚠' : 'ℹ'}
Pass type, heading, and children:
xport function Alert({ type = 'information', heading, children }) {
return (
<div>
<div>
<span role="img" aria-label={type === 'warning' ? 'Warning' : 'Information'}>
{type === 'warning' ? '⚠' : 'ℹ'}
Import and render inside your App component:
import { Alert } from './Alert';
function App() {
return <Alert />;
}
You’ve got a working component. But let’s make it smarter.
Import and render inside your App component:
import { Alert } from './Alert';
function App() {
return <Alert />;
}
You’ve got a working component. But let’s make it smarter.
export function Alert() {
return (
<div>
<div>
<span role="img" aria-label="Warning">⚠</span>
<span>Oh no!</span>
</div>
<div>Something went wrong</div>
</div>
);
}
Clean, clear, and accessible!
export function Alert() {
return (
<div>
<div>
<span role="img" aria-label="Warning">⚠</span>
<span>Oh no!</span>
</div>
<div>Something went wrong</div>
</div>
);
}
Clean, clear, and accessible!
const Button = ({ label }: { label: string }) => <button>{label}</button>;
But for larger components, a named type makes things more readable and reusable.
const Button = ({ label }: { label: string }) => <button>{label}</button>;
But for larger components, a named type makes things more readable and reusable.
Use type for simple props
Use the interface if you plan to extend it later
Example:
interface BaseProps {
id: string;
}
interface CardProps extends BaseProps {
title: string;
}
Use type for simple props
Use the interface if you plan to extend it later
Example:
interface BaseProps {
id: string;
}
interface CardProps extends BaseProps {
title: string;
}
• Type safety for function arguments
• Cleaner code with full IDE autocomplete
• No more guessing what props a component expects
• Fewer runtime bugs
• Type safety for function arguments
• Cleaner code with full IDE autocomplete
• No more guessing what props a component expects
• Fewer runtime bugs
Here’s how you type props correctly in TypeScript:
type ButtonProps = {
label: string;
onClick: () => void;
};
const Button: React.FC<ButtonProps> = ({ label, onClick }) => (
<button onClick={onClick}>{label}</button>
);
Here’s how you type props correctly in TypeScript:
type ButtonProps = {
label: string;
onClick: () => void;
};
const Button: React.FC<ButtonProps> = ({ label, onClick }) => (
<button onClick={onClick}>{label}</button>
);
They’re about creating building blocks that can scale with your app.
React + TypeScript gives you all the tools, you just need the right pattern 🧠
They’re about creating building blocks that can scale with your app.
React + TypeScript gives you all the tools, you just need the right pattern 🧠
const handleClose = () => {
setVisible(false);
if (onClose) onClose();
};
const handleClose = () => {
setVisible(false);
if (onClose) onClose();
};
const [visible, setVisible] = useState(true);
if (!visible) return null;
const [visible, setVisible] = useState(true);
if (!visible) return null;
type AlertProps = {
heading: string;
type: 'info' | 'error';
closable?: boolean;
onClose?: () => void;
};
type AlertProps = {
heading: string;
type: 'info' | 'error';
closable?: boolean;
onClose?: () => void;
};
🔹𝐏𝐫𝐨𝐩𝐬 – control content & behavior
🏁𝐒𝐭𝐚𝐭𝐞 – manage interaction
📣𝐄𝐯𝐞𝐧𝐭𝐬 – notify parent components
Let’s break it down ↓
🔹𝐏𝐫𝐨𝐩𝐬 – control content & behavior
🏁𝐒𝐭𝐚𝐭𝐞 – manage interaction
📣𝐄𝐯𝐞𝐧𝐭𝐬 – notify parent components
Let’s break it down ↓
Just show a message, maybe a color, maybe a close button.
But then someone asks:
𝘊𝘢𝘯 𝘪𝘵 𝘣𝘦 𝘥𝘪𝘴𝘮𝘪𝘴𝘴𝘪𝘣𝘭𝘦?
𝘊𝘢𝘯 𝘸𝘦 𝘤𝘶𝘴𝘵𝘰𝘮𝘪𝘻𝘦 𝘵𝘩𝘦 𝘩𝘦𝘢𝘥𝘪𝘯𝘨?
𝘊𝘢𝘯 𝘸𝘦 𝘬𝘯𝘰𝘸 𝘸𝘩𝘦𝘯 𝘪𝘵 𝘨𝘦𝘵𝘴 𝘤𝘭𝘰𝘴𝘦𝘥?
Now you’re juggling logic, props, and state
Just show a message, maybe a color, maybe a close button.
But then someone asks:
𝘊𝘢𝘯 𝘪𝘵 𝘣𝘦 𝘥𝘪𝘴𝘮𝘪𝘴𝘴𝘪𝘣𝘭𝘦?
𝘊𝘢𝘯 𝘸𝘦 𝘤𝘶𝘴𝘵𝘰𝘮𝘪𝘻𝘦 𝘵𝘩𝘦 𝘩𝘦𝘢𝘥𝘪𝘯𝘨?
𝘊𝘢𝘯 𝘸𝘦 𝘬𝘯𝘰𝘸 𝘸𝘩𝘦𝘯 𝘪𝘵 𝘨𝘦𝘵𝘴 𝘤𝘭𝘰𝘴𝘦𝘥?
Now you’re juggling logic, props, and state