Open Coteh opened 3 years ago
I also realize in #57 it was mentioned that the project did not originally ship with type definitions. Still, I thought I'd mention this as something to consider.
The docs are misleading. I prepared a PR for this #113 .
You should assign the return value of Hook
and then unhook that assignment.
This is similar to setTimeout
or setInterval
.
useEffect(() => {
- Hook(
+ const hookedConsole = Hook(
window.console,
(log) => setLogs((currLogs) => [...currLogs, log]),
false
)
- return () => Unhook(window.console)
+ return () => Unhook(hookedConsole)
}, [])
Hi,
I am just curious about the intended usage of
Unhook
method.Currently, I have the following inside my component:
This matches the usage example in the README and I think this would pass in a JavaScript project. However, in TypeScript, I need to cast
window.console
toany
type when passing it toUnhook
otherwise the following error occurs:Casting as
any
would be an acceptable solution (but not a desirable one) to me, but I also noticed thatHook
method returns the console back as aHookedConsole
object (whose type is basically the same asConsole
but withfeed
property).Is there any difference in using the return value of
Hook
and passing that intoUnhook
instead? Like this:After viewing the implementation, this should work the same as the previous, since
window.console
has thefeed
property injected into it and a reference to the same object is returned from theHook
method. But this doesn't seem to be documented. Is there a caveat to this approach? Or could the documentation be updated to reflect this approach instead? This is more desirable for a TypeScript project IMO since it avoids a cast toany
.edit: User could also cast
window.console
toHookedConsole
directly, but this requires the user to have read the code and understood what it does to thewindow.console
object to verify that this type conversion is valid. Not sure what the best approach is here from documentation standpoint.