1

Calling the WebAssembly API in an HTML file works perfectly,

<html>
  <body>
    <script>
      ...
          WebAssembly.instantiate(bytes, importObject)
       ...

   </script>
  </body>

</html>

However, calling it in a React Component, when running 'npm start' at the root of my react-app, results in this error:

'WebAssembly' is not defined  no-undef

import React from 'react';

export default class WasmContainer extends React.Component {

    constructor(props) {
        super(props);
        this.func = this.func.bind(this);
    }

    func(){
            ...
        WebAssembly.instantiate(bytes, importObject)
            ...

        return "...";
    }

    render() {
        return (<p>{this.func()}</p>)
    }
}

I want to run a .wasm file from a React Component and this is blocking me.

How to solve this problem?

3
  • WebAssembly in your React component is not defined Commented Dec 28, 2017 at 23:47
  • import it: import WebAssembly from 'web-assembly' Commented Dec 28, 2017 at 23:48
  • Are you referring to this package: web-assembly ? Commented Dec 29, 2017 at 0:04

1 Answer 1

2

This looks very much like a linting error. Are you using ESLint?

You need to inform your linter that WebAssembly is a global object. Try adding the following to the top of the file:

/* global WebAssembly */

Although a better option might be to add it to your eslint confit file.

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

Comments

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.