building blocks in React:
Hook = special function that can be called inside a component definition
possible examples:
useStateuseEffect: for triggering side effects (e.g. API queries)useQuery: more convenient way to query APIsuseTodos: custom hook for managing an array of todosinside a component defintion:
hooks must be called in the same order on every render
(React uses call order to keep track of hook identity)
some components and hooks from libraries:
<Button>, <Alert>, ...<Route>, <Link>, useParams, useNavigateuseQueryuseForm<Suspense>, useEffect, useContext, ...lists of available components and hooks:
awesome-react-components: list of components, ...
awesome-react-hooks: list of hooks
use cases:
example components for reuse (within a project or across projects):
Button (for styling)CalendarProgressBarexample components for structuring code:
TodoAppTodoListTodoItemexamples of custom hooks:
in a todo app: useTodos
in a web shop app: useProducts, useCart
using custom components and a custom hook for a todo list application:
function TodoApp() {
const todoData = useTodos();
return (
<div>
<Header />
<AddTodo onAdd={todoData.addTodo} />
<TodoList
todos={todoData.todos}
onDelete={todoData.deleteTodo}
onCompletedChange={todoData.setCompleted}
/>
<Statistics todos={todoData.todos} />
</div>
);
}