1.What are Custom Hooks in React?

They are Javascript functions that allows us to reuse stateful logic between multiple React components.

React provides built-in-hooks such as:

  • useState
  • useEffect
  • useContext
  • useReducer

we can create our own Hooks based on our requriements.

Important rules:

  • A Custom Hook name should start with use.
  • Custom Hooks can use other React Hooks.
  • They help us reuse logic across components.

Example of a Custom Hook

i

import { useState } from "react"; function useCounter() { const [count, setCount] = useState(0); const increment = () => { setCount(count + 1); }; const decrement = () => { setCount(count - 1); }; return { count, increment, decrement }; } export default useCounter; 

Here:

  • useCounter is our Custom Hook.
  • useState manages the counter value.
  • increment increases the count.
  • decrement decreases the count.
  • We return the count and functions so components can use them.
import { useState } from "react"; function useCounter() { const [count, setCount] = useState(0); const increment = () => { setCount(count + 1); }; const decrement = () => { setCount(count - 1); }; return { count, increment, decrement }; } export default useCounter; 

Here:

  • useCounter is our Custom Hook.
  • useState manages the counter value.
  • increment increases the count.
  • decrement decreases the count.
  • We return the count and functions so components can use them.

Using the Custom Hook in a Component

import useCounter from "./useCounter"; function App() { const { count, increment, decrement } = useCounter(); return ( <div> <h2>Count: {count}</h2> <button onClick={increment}>Increment</button> <button onClick={decrement}>Decrement</button> </div> ); } export default App; 

We can use the useCounter Hook in other components too.

2.What are Children Props in React?
props.children is a special prop in React that allows a component to receive content placed between its opening and closing tags.

Example:

function Card(props) { return ( <div> {props.children} </div> ); } function App() { return ( <Card> <h2>Welcome to React</h2> <p>Learning React step by step!</p> </Card> ); } export default App; Output: Welcome to React Learning React step by step! 

3.Why do we use Custom Hooks and Children Props?

**
Custom Hooks:**

  • Reuse logic between components.
  • Reduce repeated code.
  • Keep components cleaner and easier to maintain,

Children props:

  • Make components more reusable.
  • Allow flexible content inside components.
  • Help create reusable layouts,card and containers.

I learnt how Custom hooks help us reuse logic across React components and props.children allows us to pass content inside components.