Socket
Socket
Sign inDemoInstall

react-iframe-helper

Package Overview
Dependencies
3
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-iframe-helper

This is a react library meant to simplify the usage of `iframe` component in react web apps with following features: - Manage the loading status of the `iframe` content. - Simplify the communication between the parent web app and the inner app.


Version published
Maintainers
1
Install size
7.92 kB
Created

Readme

Source

This is a react library meant to simplify the usage of iframe component in react web apps with following features:

  • Manage the loading status of the iframe content.
  • Simplify the communication between the parent web app and the inner app.

Besides these two main features efforts have also been made to guaranteed security by checking the source origin of the messages.

Another thing worth mentioning for folks using typescript is that the whole lib is written in ts so types will never be a concern.

What's Under the hood

The idea is very simple. To tell if the app inside the iframe is running properly, just send a query mesage to query the status.

Once it replies then we know is's working.

If we wait for a certain amount of time without receiving the reply we see it as a loading failure.

Quick Start

Installation is fairly easy: npm install react-iframe-helper.

To use This Lib At the parent App

we will invoke the hook firstly.

const { ref, status, onLoad, send } = useIFrameParent({
                                        delay: 50 //default to 500,
                                        childDomain:'http://localhost:3006', //required
                                        listen: listenerCallback,  //optional
                                        count:10  //deafult to 10
                                        })

The count arg is about how many times we will try the status query and the delay arg is the interval in which we will send the query. For example, With the above shown code the status would be marked as failed when we wait for 500ms without receiving the reply.

Apart from that the childDomain arg is the domain of the inner app for security check.

Lastly the listen arg is used as a callback to handle the message from the inner app.

Having the useIFrameParent invoked, the remaining work is just set the ref and onLoad(returned from the useIFrameParent hook) properly for the iframe component.

<iframe
    ref={ref}
    onLoad={onLoad}
    title='forms'
    src={'http://localhost:3006'}
/>

Now in the parent app, we can check if status is equal to IFrameStatus.LOADING, IFrameStatus.LOADED or IFRameStatus.FAILED to see the status of the inner app and and send messages to the inner app with send returned from the IFrameParent hook.

To use This Lib At the inner App

Similiar to how we use the useIFrameParent hook, we send the parent domain and a listener: const { send } = useIFrameChild({parentDomain: 'http://localhost:3000', listen }).

Then messages from the parent app can be retrieved through the listen we send in and to send messages to parent app, just use the send method returned.

FAQs

Last updated on 13 Nov 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc