Components and JSX
function MyComponent({ title }) { return <h1>{title}</h1>; }Creates a functional component with props. Pitfall: always return a single root element or a Fragment.<React.Fragment>...</React.Fragment> or <>...</>Groups elements without adding extra DOM nodes. Pitfall: short syntax <> does not support the key attribute.{condition ? <PayCard /> : <PayCash />}Renders a component conditionally. Pitfall: numbers like 0 or empty strings render unexpectedly with && operator.{items.map(item => <li key={item.id}>{item.name}</li>)}Renders a list of items. Pitfall: avoid using array index as key if the list order can change.
State and Effect Hooks
const [balance, setBalance] = useState(0);Declares a local state variable. Pitfall: state updates are asynchronous, value changes on next render.setBalance(prev => prev + 10);Updates state based on previous state. Recommended for multiple updates in the same handler.useEffect(() => { fetchBalance(); }, []);Runs an effect once after mounting. Pitfall: omitting the dependency array runs the effect on every render.useEffect(() => { const t = setInterval(...); return () => clearInterval(t); }, []);Cleans up the effect before unmounting. Prevents memory leaks with timers or event listeners.
Performance and Reference Hooks
const inputRef = useRef(null);Persists a mutable value across renders without triggering a re-render. Access DOM via inputRef.current.const memoizedVal = useMemo(() => computeTotal(cart), [cart]);Caches the result of an expensive calculation. Pitfall: avoid overusing for simple operations.const handleClick = useCallback(() => { doSearch(query); }, [query]);Caches a function definition between renders. Useful when passing callbacks to optimized child components.const [state, dispatch] = useReducer(reducer, initialState);Manages complex state logic via dispatched actions. Great alternative to useState for multi-step forms.
Context and Custom Hooks
const ThemeContext = createContext('light');Creates a global context. Pitfall: all consumers re-render whenever the context value updates.<UserContext.Provider value={user}> <App /> </UserContext.Provider>Provides context value to all child components nested within the tree.const user = useContext(UserContext);Reads current context value. When working with API response payloads, validate them with JcHub JSON Formatter.
Event Handling and Forms
<button onClick={(e) => handleClick(e)}>Submit</button>Handles click events. React uses camelCase naming and wraps native browser events in SyntheticEvent.const handleSubmit = (e) => { e.preventDefault(); };Prevents default browser page reload when submitting an HTML form.<input value={name} onChange={e => setName(e.target.value)} />Creates a controlled input bound to React state. Ensures UI matches current component state.
Other resources
Need help with your website?
See the services I build your custom website.
Showcase site, online shop, management tool: let’s talk about your project.