ReactIntermediate#hooks

How do you create a custom hook?

Write a function whose name starts with 'use' and which calls other hooks, then return whatever the caller needs. It shares stateful logic, not state — each caller gets its own instance.

Example
function useToggle(initial = false) {
  const [on, setOn] = useState(initial);
  return [on, () => setOn(o => !o)];
}
const [open, toggle] = useToggle();

Related Questions

1
ReactIntermediate#effects#data

How do you fetch data in a React component?

Open
2
ReactAdvanced#effects#debugging

What causes an infinite re-render loop?

Open
3
ReactIntermediate#state#patterns

What is lifting state up?

Open