Discover, save, and share beautiful code snippets. Build your personal collection of reusable code.
Total Snippets
4
Languages
4
Categories
4
Basic state management in React functional components
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}Perfect centering with flexbox
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.centered-content {
text-align: center;
padding: 2rem;
background: white;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}Elegant way to create lists in Python
# Basic list comprehension
squares = [x**2 for x in range(10)]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# With condition
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # [0, 4, 16, 36, 64]
# Nested comprehension
matrix = [[i*j for j in range(3)] for i in range(3)]
print(matrix) # [[0, 0, 0], [0, 1, 2], [0, 2, 4]]Defining object shapes with TypeScript interfaces
interface User {
id: number;
name: string;
email: string;
isActive?: boolean;
roles: string[];
}
interface ApiResponse<T> {
data: T;
status: 'success' | 'error';
message?: string;
}
// Usage
const user: User = {
id: 1,
name: 'John Doe',
email: 'john@example.com',
roles: ['admin', 'user']
};
const response: ApiResponse<User> = {
data: user,
status: 'success'
};