React useReducer Hook

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
Search for a command to run...

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
No comments yet. Be the first to comment.
Add it to the boot method of the AppServiceProvider of the app to improve Laravel developmet experience. use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; class AppServiceProvider extends ServiceProvider { //... pub...

There is a plugin available for reactjs that one can use to add Calendly to Nextjs i.e., react-calendly. The problem is if you want to customize the button to open popup or modal then you need to have Calendly's pro plan for it. Here is the alternati...

After shifting to Ubuntu from Windows, I constantly look up for the alternatives of the tools that are available in Windows to enhance my arsenal in Linux. DbGate is the smartest SQL+noSQL database client. It works on Windows, Linux, MacOS and in web...

<div className="ratio ratio-16x9"> <video width="900" height="650" muted playsInline autoPlay loop> <source src="/images/test.mp4" type="video/mp4" /> </video> </div>

Creating middleware in Laravel is a common task that helps to filter HTTP requests entering your application. Middleware can perform various tasks such as authentication, logging, and modifying request/response objects. Step 1: Create Middleware You ...

The useReducer hook is a built-in hook in React that allows you to manage complex state logic in your application. It's an alternative to the useState hook that provides more flexibility and control over state management.
The useReducer hook takes two arguments: a reducer function and an initial state. The reducer function is responsible for updating the state based on actions that are dispatched to it. The initial state argument is the starting value for the state.
Here's an example of how to use the useReducer hook:
import React, { useReducer } from 'react';
const initialState = { count: 0 };
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: 'increment' })}>+</button>
<button onClick={() => dispatch({ type: 'decrement' })}>-</button>
</>
);
}
In this example, we define an initial state object with a count property set to 0. We also define a reducer function that takes a state and an action as arguments and returns a new state based on the action type.
In the Counter component, we call the useReducer hook with the reducer function and initialState as arguments. This returns an array with two values: the current state and a dispatch function. The dispatch function is used to send actions to the reducer, which updates the state accordingly.
Finally, we render the current count value and two buttons that dispatch increment and decrement actions when clicked, respectively.
That's a simple example of how to use the useReducer hook in React. With this hook, you can handle more complex state updates in your application with ease.