Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access iframe console in React using hooks #98

Open
Arshanshagarwal opened this issue Jan 13, 2022 · 1 comment
Open

How to access iframe console in React using hooks #98

Arshanshagarwal opened this issue Jan 13, 2022 · 1 comment

Comments

@Arshanshagarwal
Copy link

Context - I wanted to capture the console of the iframe element I have created and use those logs to showcase a console in the browser

Code -

import React, { useState, useEffect } from 'react'
import { Console, Hook, Unhook } from 'console-feed'

const LogsContainer = ({iframeRef}) => {
  const [logs, setLogs] = useState([])

  // run once!
  useEffect(() => {
    Hook(
      iframeRef.current.contentWindow.console,
      (log) => setLogs((currLogs) => [...currLogs, log]),
      false
    )
    return () => Unhook(iframeRef.current.contentWindow.console)
  }, [])

  return <Console logs={logs} variant="dark" />
}

export { LogsContainer } 

Issue - I tried using Reference of iFrame to access the console, by passing the iframRef from the main code to the Hook and unhooking the same after using it. But it does not work, my logs state that was returned didn't have any elements in it. I tried using the same code for window.console, and it worked perfectly fine.

How could I use it?

@isaacking5
Copy link

The same case has also come up for me, does anyone have any ideas how to work around it?

Please let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants