# Preact > Components represent the basic building block in Preact. They are fundamental in making it easy to build complex UIs from little building blocks. They're also responsible for attaching state to our re --- # Source: https://preactjs.com/guide/v10/components#error-boundaries # Source: https://preactjs.com/guide/v10/components#fragments # Components Components represent the basic building block in Preact. They are fundamental in making it easy to build complex UIs from little building blocks. They're also responsible for attaching state to our rendered output. There are two kinds of components in Preact, which we'll talk about in this guide. --- * [Functional Components](#functional-components) * [Class Components](#class-components) * [Lifecycle Methods](#lifecycle-methods) * [Error Boundaries](#error-boundaries) * [Fragments](#fragments) --- ## Functional Components Functional components are plain functions that receive `props` as the first argument. The function name **must** start with an uppercase letter in order for them to work in JSX. ```jsx function MyComponent(props) { return
Something went badly wrong
; } return props.children; } } ``` ### Fragments A `Fragment` allows you to return multiple elements at once. They solve the limitation of JSX where every "block" must have a single root element. You'll often encounter them in combination with lists, tables or with CSS flexbox where any intermediate element would otherwise affect styling. ```jsx import { Fragment, render } from 'preact'; function TodoItems() { return ({char}
))]{name}, Age: {age}
); })}Counter: {state.value}
Counter: {value}
Counter A: {value}
I'm a nice counter
Count: {count}
Active theme: {theme}
; } // ...later function App() { return (Window width: {width}
; } ``` > The cleanup function is optional. If you don't need to run any cleanup code, you don't need to return anything in the callback that's passed to `useEffect`. ### useLayoutEffect Similar to [`useEffect`](#useeffect), `useLayoutEffect` is used to trigger side-effects but it will do so as soon as the component is diffed and before the browser has a chance to repaint. Commonly used for measuring DOM elements, this allows you to avoid flickering or pop-in that may occur if you use `useEffect` for such tasks. ```jsx import { useLayoutEffect, useRef } from 'preact/hooks'; function App() { const hintRef = useRef(null); useEffect(() => { const hintWidth = hintRef.current.getBoundingClientRect().width; // We might use this width to position and center the hint on the screen, like so: hintRef.current.style.left = `${((window.innerWidth - hintWidth) / 2}px`; }, []); return (This is a hint
{error.message}