Installation
npm install use-megamind
or
yarn add use-megamind
Motivation
I wanted to make a custom solution specially/not only for data fetching but also for any kinds of async operation. Of course, React Query ( GitHub - TanStack/query: 🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query. ) is awesome ( even though I haven’t tried it yet ) but I wanted to make something thats easier to implement, specially on an existing project & also doesn’t need a lot of documentations to follow. Something that’s small but does the job.
How to use
Example 1: Async function without parameters
"use client"
import useMegamind from "use-megamind"
function testAsyncFunction1() {
return new Promise((res, rej) => {
try {
setTimeout(() => {
res("Hello world")
}, 1000);
}
catch (e) {
rej(e)
}
})
}
export default function Home() {
const { data, error, isLoading, call, clear } = useMegamind(testAsyncFunction1)
return <div className="flex flex-col gap-y-10">
<p>{JSON.stringify(data)}</p>
</div>
}
Example 2: Async function with parameter(s)
functionParams: The async function parameters, one by one ( Default: null )
Yes, You will get intellisense like this
"use client"
import useMegamind from "use-megamind"
function testAsyncFunction1(ms: number) {
return new Promise((res, rej) => {
try {
setTimeout(() => {
res("Hello world")
}, ms);
}
catch (e) {
rej(e)
}
})
}
export default function Home() {
const { data, error, isLoading, call, clear } = useMegamind(testAsyncFunction1, {
functionParams: [1000]
})
return <div className="flex flex-col gap-y-10">
<p>{JSON.stringify(data)}</p>
</div>
}
Example 3: Async function call on a button click
callRightAway: Whether the async function should be called on component mount ( Default: true )
"use client"
import useMegamind from "use-megamind"
function testAsyncFunction1(ms: number) {
return new Promise((res, rej) => {
try {
setTimeout(() => {
res("Hello world")
}, ms);
}
catch (e) {
rej(e)
}
})
}
export default function Home() {
const { data, error, isLoading, call, clear } = useMegamind(testAsyncFunction1, {
options: {
callRightAway: false,
}
})
return <div className="flex flex-col gap-y-10">
<p>{JSON.stringify(data)}</p>
<button onClick={() => {
call(1000)
}}>
Call
</button>
</div>
}
Yes, passing
functionParams
is optional, unless you want to call the function on component mount
Example 4: Adding options
maxCalls: Maximum how many times the async function can be called ( Default: ‘infinite’ ). If maxCalls is 1, it caches the response.
minimumDelayBetweenCalls: Minimum delay between two calls for the async function ( Default: 0 ms )
debug: Show/hide logs ( Default: false )
callRightAway: Whether the async function should be called on component mount ( Default: true )
"use client"
import useMegamind from "use-megamind"
function testAsyncFunction1(ms: number) {
return new Promise((res, rej) => {
try {
setTimeout(() => {
res("Hello world")
}, ms);
}
catch (e) {
rej(e)
}
})
}
export default function Home() {
const { data, error, isLoading, call, clear } = useMegamind(testAsyncFunction1, {
options: {
callRightAway: false,
debug: false,
maxCalls: 1,
minimumDelayBetweenCalls: 1000 // ms
}
})
return <div className="flex flex-col gap-y-10">
<p>{JSON.stringify(data)}</p>
<button onClick={() => {
call(1000)
}}>
Call
</button>
</div>
}
Example 5: Listening to the async function events
"use client"
import useMegamind from "use-megamind"
function testAsyncFunction1(ms: number) {
return new Promise((res, rej) => {
try {
setTimeout(() => {
res("Hello world")
}, ms);
}
catch (e) {
rej(e)
}
})
}
export default function Home() {
const { data, error, isLoading, call, clear } = useMegamind(testAsyncFunction1, {
options: {
callRightAway: false,
debug: false,
maxCalls: 1,
minimumDelayBetweenCalls: 1000 // ms
},
events: {
onError(error) {
// set your states here if needed
},
onLoadingChange(isLoading) {
// set your states here if needed
},
onLoadingFinished() {
// set your states here if needed
},
onLoadingStart() {
// set your states here if needed
},
onSuccess(data) {
// set your states here if needed
},
}
})
return <div className="flex flex-col gap-y-10">
<p>{JSON.stringify(data)}</p>
<button onClick={() => {
call(1000)
}}>
Call
</button>
</div>
}
Example 6: Other things
clear: Clears the state of the hook. Resets data, error, and loading states.
reset: Resets the state of the hook including cache and call counter. Clears data, error, and loading states. Clears the cached result and resets the call counter.
"use client"
import useMegamind from "@/lib/useMegamind";
function testAsyncFunction1(ms: number): Promise<string> {
return new Promise((res, rej) => {
try {
setTimeout(() => {
res("Hello world")
}, ms);
}
catch (e) {
rej(e)
}
})
}
export default function Home() {
const { data, error, isLoading, call, clear, reset } = useMegamind(testAsyncFunction1, {
options: {
callRightAway: false,
debug: false,
maxCalls: 1,
minimumDelayBetweenCalls: 1000 // ms
},
events: {
onError(error) {
// set your states here if needed
},
onLoadingChange(isLoading) {
// set your states here if needed
},
onLoadingFinished() {
// set your states here if needed
},
onLoadingStart() {
// set your states here if needed
},
onSuccess(data) {
// set your states here if needed
},
}
})
return <div className="flex flex-col gap-y-10">
<p>{JSON.stringify(data)}</p>
<button onClick={() => {
call(1000)
}}>
Call
</button>
<button onClick={() => {
clear()
}}>
Clear
</button>
<button onClick={() => {
reset()
}}>
Reset
</button>
</div>
}
if you just want to clear the states and keep the cache, use clear()
but to clear everything including cache use reset()
& Finaly, If you’re using TypeScipt, if you define the return types in the async function, you will get better intellisense like this: