React Custom Hooks

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 ...

React custom hooks are reusable functions in React that allow you to encapsulate and share logic between components. They follow a specific naming convention, starting with the word "use," and they can be used to abstract away complex state management, side effects, or any other behavior.
To create a custom hook, you can use existing React hooks or combine multiple hooks to build your logic. Custom hooks should return a value or an array of values that can be used by the component using the hook.
Here's an example of a custom hook that manages a counter:
import { useState } from 'react';
const useCounter = (initialValue) => {
const [count, setCount] = useState(initialValue);
const increment = () => {
setCount(count + 1);
};
const decrement = () => {
setCount(count - 1);
};
return [count, increment, decrement];
};
export default useCounter;
You can then use this custom hook in your components like this:
import React from 'react';
import useCounter from './useCounter';
const CounterComponent = () => {
const [count, increment, decrement] = useCounter(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
};
export default CounterComponent;
By creating custom hooks, you can extract and reuse complex logic across multiple components, improving code reusability and keeping your components clean and focused on their specific responsibilities.