Created
October 9, 2020 21:13
-
-
Save banagale/a2e653a35dbae87563b68e8575ade47e to your computer and use it in GitHub Desktop.
A pattern for outputting debug from a custom react hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from "react"; | |
export function useCustomHookDebugPattern(arg1, arg2, arg3, message, something, setSomething) { | |
// Watch for loop progress | |
const [count, setCount] = useState(1); | |
// Additional local state | |
const [prevMessageId, setPreviousMessageId] = useState(undefined); | |
useEffect(() => { | |
// Increment hook count | |
setCount(count => count + 1); | |
// Set any additional local state | |
setPreviousMessageId(message.msgId); | |
// Clearly indicate hook name and number of times run | |
console.log('--------------useCustomHookDebugPattern--------------'); | |
console.log(count); | |
// Output any relavent additional local state | |
console.log(prevMessageId); | |
console.log(message); | |
// Carefully condition a key state setter | |
if (arg1 !== undefined) { | |
if (arg1 === arg2.id && arg3.length === 0) { | |
setSomething(something => [...something, message]); | |
} else if (prevMessageId !== message.msgId arg3.length > 0) { | |
setSomething(something => [...something, message]); | |
} | |
} | |
}, [message]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment