0

Apologies if this is very basic as I am a complete react beginner, I'm asking this question here as I have no idea where to start to get this working.

Im trying to convert this raw html code into a react app.

<body>
    <div style="width: 600px; height: 400px;" id="aww-wrapper">
    </div>
    <script src="https://awwapp.com/static/widget/js/aww3.min.js">
    </script>
    <script type="text/javascript">
        var aww = new AwwBoard('#aww-wrapper', {
        });
    </script>
</body>

This code creates a small whiteboard app on the webpage.

I have tried to implement it into the app component in react but am having trouble getting the 'var aww = ...' script to work.

import React from 'react';
import './App.css';
import ScriptTag from 'react-script-tag'

function App() {
  return (
    <div className="App">

      <div style={{width:'600px', height:'400px'}} id="aww-wrapper">
      </div>

      <ScriptTag src="https://awwapp.com/static/widget/js/aww3.min.js" />

      <ScriptTag type="text/javascript">
        var aww = new AwwBoard('#aww-wrapper', {
        });
      </ScriptTag>

    </div>
  );
}

export default App;

I am using scripttag to get the external javascript onto the web page, but it isnt working for the 'var aww = ...' part.

It would be a huge help if anyone has a general direction that they can direct me to in order to implement the javascript into the react code.

4
  • what do you want to achieve here? why not simply make use of jsx? {var aww = new AwwBoard('#aww-wrapper', { });} Commented Aug 19, 2020 at 13:15
  • Rather than using directly under the ScriptTag. Create a new js file and put all you AwwBoard code in that. And use <Script src="./aww.js" /> Commented Aug 19, 2020 at 13:15
  • Hi messerbill, I have tried using that implementation but the code returns 'AwwBoard is not defined', i think this is happening because react is trying to compile it before it is able to grab the external js files from awwapp.com Commented Aug 20, 2020 at 4:37
  • Did you manage with this problem? Commented Aug 22, 2020 at 5:20

2 Answers 2

1

You will need to create an initialing function wrapper for the AwwBoard. The function will create and configure a script object. The AwwBoard function will return a promise to the caller, which will provide the AwwBoard instance for later use (to access its methods) once the javascript has executed.

We also want to pass config params as well as a mount Ref for the component.

To achieve this, I created the following function which takes a ref (for the div you want to mount the AwwBoard) as well as configuration params. It loads the script, and resolves the promise with the instance.

const AwwBoard = (mountRef: React.Ref, params: Object = {}) => {
  let promise = new Promise(function (resolve, reject) {
    var script = document.createElement("script");
    script.src = "https://awwapp.com/static/widget/js/aww3.min.js";

    script.onload = function () {
      //AwwBoard should be available on the window object after loading the script
      const { AwwBoard } = window;
      //create a new instance
      var aww = new AwwBoard(`#${mountRef.id}`, params);
      //resolve the promise and return the instance.
      resolve(aww);
    };

    document.getElementsByTagName("head")[0].appendChild(script);
  });

  return promise;
};

export default AwwBoard;

Now we set up a div with a react ref, and a useEffect which loads the AwwBoard on "componentDidMount"

import React, { useEffect } from "react";
import "./styles.css";
import AwwBoard from "./AwwBoard";

export default function App() {
  const awwBoardInstance = React.useRef(); //access methods on the board here
  const awwBoardMountRef = React.useRef(null); //mounting ref for the AwwBoard

  useEffect(() => {
    AwwBoard(awwBoardMountRef.current, {}).then((instance) => {
      awwBoardInstance.current = instance;
    });
  },[]);

  return (
    <div className="App">
      <div
        style={{ width: "600px", height: "400px" }}
        id="aww-wrapper"
        ref={awwBoardMountRef}
      ></div>
    </div>
  );
}

CodeSandbox

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for your help! This worked perfectly, it was sorting out the promises that fixed it.
0

It would be better if you bundled the aww3.min.js into your app and then did something like this:

import React from 'react';
import { useEffect } from "react";
import './App.css';
import './aww3.min.js'

function App() {
  useEffect(() => {
    new AwwBoard('#aww-wrapper', {
        });
  }, [])

  return (
    <div className="App">

      <div style={{width:'600px', height:'400px'}} id="aww-wrapper">
      </div>
    </div>
  );
}

export default App;

2 Comments

Hi Goliy, thanks for the response, I have tried to implement that but I need to grab the aww.min.js file externally instead of hosting it in my app. However, even if I grab the file externally, the new AwwBoard... code returns 'AwwBoard' is not defined, I think this is a different problem on its own, im not sure why that piece of code works in a script tag but not when its implemented in useEffect.
If you grab the file externally you have to wait until the script will be evaluated and registered an AwwBoard in global visibility area. That's why you have a "not defined"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.