React Unit 4 QB Sol
React Unit 4 QB Sol
**Example Scenario:**
```jsx
import React, { useState } from 'react';
function Counter() {
// Declare a state variable named 'count' and initialize it to 0
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
**Explanation:**
1. **Declaring State:**
In the `Counter` component, we declare a state variable named
`count` using the `useState` hook. The `useState` hook takes the
initial value of the state variable as an argument (in this case, `0`),
and returns an array with two elements: the current value of the
state variable (`count`), and a function to update its value
(`setCount`).
2. **Updating State:**
When the user clicks the "Increment" button, the `onClick` event
handler calls the `setCount` function with the new value of the
`count` state variable (`count + 1`). This triggers a re-render of the
component with the updated state value.
function Timer() {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
const intervalId = setInterval(() => {
setSeconds(prevSeconds => prevSeconds + 1);
}, 1000);
return () => {
clearInterval(intervalId);
};
}, []); // Dependency array is empty, meaning the effect runs only
once on component mount
return (
<div>
<p>Elapsed Time: {seconds} seconds</p>
</div>
);
}
**Explanation:**
**1. `useState`:**
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
```
**2. `useReducer`:**
- **Complex State Logic:** `useReducer` is suitable for managing
complex state logic or state that has a more intricate structure,
such as nested objects or arrays.
- **Predictable State Updates:** Unlike `useState`, `useReducer`
enforces a more predictable update pattern by dispatching actions
to update the state based on the previous state. This can be helpful
for managing state transitions in a more controlled manner.
- **Centralized State Logic:** `useReducer` encourages a more
centralized approach to state management by defining a reducer
function that specifies how state transitions should occur. This can
make it easier to understand and reason about the state logic,
especially in larger applications.
```jsx
import React, { useReducer } from 'react';
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
<p>Count: {state.count}</p>
<button onClick={() => dispatch({ type:
'increment' })}>Increment</button>
</div>
);
}
```
**Advantages of `useReducer`:**
1. **Centralized State Logic:** `useReducer` promotes a
centralized approach to state management, making it easier to
understand and maintain complex state transitions.
2. **Predictable Updates:** With `useReducer`, state updates are
more predictable and deterministic, as they are based on
dispatched actions.
3. **Suitable for Complex State:** `useReducer` is better suited for
managing complex state or state with a more intricate structure.
**Advantages of `useState`:**
**Limitations of `useReducer`:**
**Limitations of `useState`:**