// Master React development including Hooks, Server Components, state management, testing, and performance optimization. Use for building modern React applications with best practices.
| name | react-development |
| description | Master React development including Hooks, Server Components, state management, testing, and performance optimization. Use for building modern React applications with best practices. |
Expert React patterns and production-ready applications.
import React, { useState, useCallback } from 'react';
export const Counter: React.FC<{ initial?: number }> = ({ initial = 0 }) => {
const [count, setCount] = useState(initial);
const increment = useCallback(() => setCount(c => c + 1), []);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>+</button>
</div>
);
};
function useFetch<T>(url: string): { data: T | null; loading: boolean; error: Error | null } {
const [state, setState] = useState({ data: null, loading: true, error: null });
useEffect(() => {
fetch(url)
.then(r => r.json())
.then(data => setState({ data, loading: false, error: null }))
.catch(error => setState({ data: null, loading: false, error }));
}, [url]);
return state;
}
export default async function PostList() {
const posts = await fetchPosts(); // Server-side only
return (
<ul>
{posts.map(post => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}
const ThemeContext = createContext<'light' | 'dark'>('light');
function ThemeProvider({ children }) {
const [theme, setTheme] = useState<'light' | 'dark'>('light');
return (
<ThemeContext.Provider value={theme}>
{children}
</ThemeContext.Provider>
);
}
class ErrorBoundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError(error) {
return { hasError: true };
}
render() {
return this.state.hasError ? <h1>Error occurred</h1> : this.props.children;
}
}
// Memoization
const MemoizedComponent = React.memo(({ data }) => {
return <div>{data.title}</div>;
});
// useCallback for callbacks
const handleClick = useCallback(() => {
doSomething();
}, []);
// useMemo for expensive calculations
const value = useMemo(() => expensiveComputation(data), [data]);
import { render, screen } from '@testing-library/react';
test('increments counter', () => {
render(<Counter initial={0} />);
const button = screen.getByRole('button');
fireEvent.click(button);
expect(screen.getByText('Count: 1')).toBeInTheDocument();
});