Skip to content

Instantly share code, notes, and snippets.

@imhalid
Created January 18, 2023 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imhalid/abc87e71523c0446245f1ab24d8cb2d5 to your computer and use it in GitHub Desktop.
Save imhalid/abc87e71523c0446245f1ab24d8cb2d5 to your computer and use it in GitHub Desktop.
Sending console output into an HTML element
import { useEffect } from 'react'
const ConsoleLog = () => {
useEffect(() => {
if (typeof console !== 'undefined') {
if (typeof console.log !== 'undefined') {
console.olog = console.log
} else {
console.olog = function () {}
}
console.log = function (message) {
let consolelog = document.querySelector('#consolelog')
let p = document.createElement('p')
p.innerHTML = message
consolelog.appendChild(p)
}
console.error = console.debug = console.info = console.log
}
}, [])
return <p id='consolelog'></p>
}
export default ConsoleLog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment