
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-modern-hooks
Advanced tools
React modern hooks is a library for customized and reusable hooks. These hooks depend on react library under the hood.
Disclaimer: atleast react version 16.8 is required for the hooks
npm install react-modern-hooks
or
yarn add react-modern-hooks
Having issues setting up or using this hooks library, file a bug report here
You can find the complete React Modern Hooks documentation on the website
useFetch - Hook for fetching/refetching data from an API endpointuseNetwork - Hook for getting the network statususeFullScreen - Hook to toggle a given HTMLElement to fullscreen and exit fullscreenuseGeolocation - Hook to get a users current geographic locationuseSelectedText - Hook to get the highlighted text on a pageuseCopyToClipboad - Hook to copy text to clipboaduseStateCallback - Hook that acts as a state callback i.e. functionality same as react class-based setState that provides a fallback with your current set stateuseResize - Hook to handle page resizinguseSearch - Hook to allow delayed search and only search after a user releases all keys for set timeoutuseImageDownload - Hook that allows download of images from a given urluseFocus - Hook to autofocus input and/or textarea componentsuseDebounce - Hook to for delayed callback functionsuseStorage - Hook to allow use of localstorageuseOs - Hook to get the current OS of the userAgentusePageTitle - Hook to update the page title of a document urluseOnline - Hook to check if user is currently online or offlineuseDeviceDetect - Hook to detect the device a user is using and/or if either mobile or notWe have several examples and complete guide on the website. Here are a few to get you started:
Hook for fetching/refetching data from an API endpoint
import { useFetch } from 'react-modern-hooks';
const Demo = () => {
const { data, loading, refetch, processRequest, error } = useFetch('https://jsonplaceholder.typicode.com/todos');
if (loading) return <p>Loading...</p>;
if (error || !data) return <p>{error}</p>;
const handleRequest = () => {
processRequest('https://jsonplaceholder.typicode.com/todos', {
method: 'POST',
});
};
return (
<div>
<p>Data Fetched</p>
<button onClick={refetch}>Refetch</button>
<button onClick={handleRequest}>Click to Process New Request</button>
</div>
);
};
url - The API endpoint to fetch data from
options - optional parameters allowed to be passed when fetching data from an endpoint
data - returned data from the api endpoint
error - Error response returned incase something goes wrong during data fetching
loading - Loading state returned when data is still loading from the API endpoint
refetch - Refetch function to refetch data
processRequest - A helper function to process requests made via POST or PATCH or DELETE methods
A React Hook that gets the users network information and which provides information about the connection a device is using to communicate with the network and provides a means for scripts to be notified if the connection type changes
import { useNetwork } from 'react-modern-hooks';
const Demo = () => {
const { connection } = useNetwork();
connection.onchange = (e) => {
// Handle change of connection type here.
};
return (
<div>
<h1>Network Info</h1>
<p>Downlink: {connection.downlink} </p>
<p>Downlink Max: {connection.downlinkMax}</p>
<p>Round trip time: {connection.rtt}</p>
<p>Has user set a reduced data usage option on the user agent: {connection.saveData}</p>
<p>Connection Type: {connection.type}</p>
</div>
);
};
connection - The connection response of the network
A React hook that enables a HTMLElement or component to toggle into a fullscreen and/or exit fullscreen. This hooks provides the open, close and toggle methods which can be used to switch the screen from open to closed or viceversa.
import { useRef } from 'react';
import { useFullScreen } from 'react-modern-hooks';
const Demo = () => {
const ref = useRef(null);
const { fullScreen, close, open, toggle, error } = useFullScreen(ref);
if (error) {
console.log('Error: ', error);
}
return (
<div>
<div ref={ref} style={{ background: '#fff' }}>
<h1> This is the div (one with ref) that will enter fullscreen mode</h1>
{fullScreen ? <p>Only visible in fullscreen mode</p> : <p>Not in fullscreen mode</p>}
<button onClick={() => open()}>Open FullScreen</button>
<button onClick={() => close()}>Exit FullScreen</button>
<button onClick={() => toggle()}>Toggle Open/Exit FullScreen</button>
</div>
</div>
);
};
ref - A HTMLElement mutable ref object to toggle to fullscreen
onExit - An optional callback function to run when exiting fullscreen
fullScreen - A boolean value to show if fullscreen or not
open - A function to open the referenced component/element to fullscreen window
close - A function to close/exit the referenced component/element from fullscreen window
toggle - A function to both open and/or close/exit the referenced component/element to and from fullscreen window
error - An error message incase something doesn't go right on opening and/or closing fullscreen window
A react hook which gets a users/agent's current geographic location. It return both the city, ip, country and other location variables useful in determining a users position on the world map
import { useGeolocation } from 'react-modern-hooks';
const Demo = () => {
const {
loading,
error,
userIP,
city,
country,
latitude,
longitude,
region,
location, //Object that comes with other properties on top
} = useGeolocation();
if (error) {
console.log('Error: ', error);
}
if (loading) return <p>Loading...</p>;
return (
<div>
<p>User IP Address: {userIP}</p>
<p>City: {city}</p>
<p>Country: {country}</p>
<p>Region: {region}</p>
<p>Longitude: {longitude}</p>
<p>Latitude: {latitude}</p>
<p>Location Timezone: {location?.timezone}</p>
</div>
);
};
location - Location object with all other user details
latitude - Current latitude of a user/agent's position
longitude - Current longitude of a user/agent's position
userIP - Current IP address of a user/agent
city - City the user/agent is currently located in
region - Region the user/agent is currently located in
country - Country the user/agent is currently located in
error - Any error occurring during geolocating a user/agent
loading - A boolean variable indicating if a users location is still being fetched
A React Hook that allows copying of text to clipboad. Providing the capability to copy text from one medium to another. The hook provides a copyText method which can be used to copy the text
import { useCopyToClipboard } from 'react-modern-hooks';
const Demo = () => {
const { copiedText, copyText, error, copied } = useCopyToClipboard();
console.log('is text copied', copied);
return (
<div>
{error ? <p>Error copying text: {error}</p> : null}
<button onClick={() => copyText('Copy this text to clipboard')}>Copy Text to clipboard</button>
</div>
);
};
error - Error message that occured during copy
copied - Boolean state if the text was copied successfully
copiedText - The copied text
copyText - A function to copy the text
FAQs
A react modern hooks library for customized and reusable hooks
The npm package react-modern-hooks receives a total of 11 weekly downloads. As such, react-modern-hooks popularity was classified as not popular.
We found that react-modern-hooks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.