Shūn.AI

Online
AI

Hi! I'm your AI assistant. How can I help you today?

Can you explain what a React hook is?

U
AI

Sure! React hooks are functions that let you use state and other React features in functional components.

Here's a simple example of the useState hook:

jsx
1import React, { useState } from 'react';
2
3function Counter() {
4  const [count, setCount] = useState(0);
5
6  return (
7    <div>
8      <p>You clicked {count} times</p>
9      <button onClick={() => setCount(count + 1)}>
10        Click me
11      </button>
12    </div>
13  );
14}

Popular React hooks include:

  • useState: For managing state
  • useEffect: For side effects
  • useContext: For accessing context
  • useRef: For persistent mutable values

You can also create your own custom hooks to reuse stateful logic between components.

Try asking me about:

Press Enter to send, Shift + Enter for new line

Type / for commands