👉 Tài liệu TIẾNG VIỆT (Vietnamese Documents): XEM NGAY
react-crazy-hooks
is a collection of custom hooks for React, designed to simplify common tasks in both JavaScript and TypeScript projects. The library currently includes the following hooks:
useWindowDimensions
useClipboard
useIpAddress
useModalState
useURLParams
useLocalStorage
useFetch
useDebounce
usePrevious
useOnClickOutside
More hooks will be added in future updates.
You can install react-crazy-hooks via bun, npm or yarn:
bun add react-crazy-hooks
npm install react-crazy-hooks
yarn add react-crazy-hooks
This hook provides the current width and height of the window.
import { useWindowDimensions } from "react-crazy-hooks";
const WindowDimensionsComponent = () => {
const { width, height } = useWindowDimensions();
return (
<div>
<p>Width: {width}px</p>
<p>Height: {height}px</p>
</div>
);
};
This hook allows you to copy text to the clipboard.
import { useClipboard } from "react-crazy-hooks";
const ClipboardComponent = () => {
const { copied, copyToClipboard } = useClipboard();
return (
<div>
<button onClick={() => copyToClipboard("React Crazy Hooks")}>
Copy to Clipboard
</button>
{copied && <p>Copied: {copied}</p>}
</div>
);
};
This hook fetches the user’s IP address.
import { useIpAddress } from "react-crazy-hooks";
const IpAddressComponent = () => {
const { ipAddress, error, isLoading } = useIpAddress();
return (
<div>
<p>Your IP Address: {isLoading ? "Loading..." : ipAddress.ip}</p>
<p>Your Country: {isLoading ? "Loading..." : ipAddress.country}</p>
</div>
);
};
Debounce a value.
import { useDebounce } from "react-crazy-hooks";
function App() {
const [searchTerm, setSearchTerm] = useState("");
const debouncedSearchTerm = useDebounce(searchTerm, 500);
// Use debouncedSearchTerm for API calls or other side effects
return (
<div>
<input
type="text"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
<p>Debounced Value: {debouncedSearchTerm}</p>
</div>
);
}
This hook manages the state of a modal.
import { useModalState } from "react-crazy-hooks";
const ModalComponent = () => {
const { visible, closeModal, openModal, toggle } = useModalState();
return (
<div>
<button onClick={openModal}>Open Modal</button>
{visible && (
<div>
<p>This is a modal!</p>
<button onClick={closeModal}>Close Modal</button>
</div>
)}
</div>
);
};
This hook provides easy access to URL parameters.
import { useURLParams } from "react-crazy-hooks";
const URLParamsComponent = () => {
const params = useURLParams();
console.log("Query Parameters: ", params);
return <div></div>;
};
Manage state with Local Storage.
import { useLocalStorage } from "react-crazy-hooks";
function App() {
const [name, setName] = useLocalStorage("name", "John Doe"); // "name" = key, "John Doe" = initialValue
return (
<div>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<p>{name}</p>
</div>
);
}
Fetch data from an API.
import { useFetch } from "react-crazy-hooks";
function App() {
const { data, loading, error } = useFetch("https://api.thatthuvi.com/data");
if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error.message}</p>;
return (
<div>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
Store the previous value of a variable.
import { usePrevious } from "react-crazy-hooks";
function App() {
const [count, setCount] = useState(0);
const prevCount = usePrevious(count);
useEffect(() => {
console.log(`Current: ${count}, Previous: ${prevCount}`);
}, [count]);
return (
<div>
<button onClick={() => setCount(count + 1)}>Increment</button>
<p>Current Count: {count}</p>
<p>Previous Count: {prevCount}</p>
</div>
);
}
Detect clicks outside of a specified element.
import { useOnClickOutside } from "react-crazy-hooks";
function App() {
const [isOpen, setIsOpen] = useState(false);
const ref = useRef(null);
useOnClickOutside(ref, () => setIsOpen(false));
return (
<div>
<button onClick={() => setIsOpen(true)}>Open Menu</button>
{isOpen && (
<div ref={ref}>
<p>Click outside of this box to close it.</p>
</div>
)}
</div>
);
}
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Nguyen Phuc Bao Chau - GitHub Profile
This project is licensed under the MIT License.