Reputation: 12423
I want to use javascript simple component in React.
for example wavesurfer.js
It is easy to use, if you don't use react.
<head>
<script src="https://unpkg.com/wavesurfer.js"></script>
</head>
<script>
let wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple'
});
</script>
<html>
<div id="waveform"></div>
</html>
It works well only this code.
So,I try to do the same thing in React.
I put <script src="https://unpkg.com/wavesurfer.js"></script>
in public/index.html
and then made class.
class Waveform extends Component {
componentDidMount(){
let wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple'
});}
render() {
return (
<div id="waveform"></div>
);
}
};
However, it shows error
'WaveSurfer' is not defined no-undef
In my understanding, wavesurfer.js
is read from CDN in head
Why WaveSurfer
class is not found??
my index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<script src="https://unpkg.com/wavesurfer.js"></script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Upvotes: 2
Views: 313
Reputation: 6336
My recommendation to you is simple. Install the NPM package. Will save you a lot of time.
Upvotes: 0
Reputation: 36
I would advise you to do a var require instead :
var WaveSurfer = require('wavesurfer.js')
Maybe it could be more efficient than the unpkg script Who must have crashed the extraction of the package and who could not load it. It can only be an error coming from the loading of the library which could not be carried out. You can use node js or yarn in particular to install this library.
npm install wavesurfer.js --save
# or
yarn add wavesurfer.js
Then simply import the library and use it as you see fit with the available variables :
import WaveSurfer from 'wavesurfer.js';
var WaveSurfer = require('wavesurfer.js');
define(['WaveSurfer'], function(WaveSurfer) {
// ... code
});
If that didn't really help you, please re-read the API site below, hoping it was a great help. https://wavesurfer-js.org/api/
Upvotes: 2
Reputation: 7767
That error is from an eslint rule. Add a comment above the relevant line: // eslint-disable-next-line no-undef
, or add it to your globals in your eslint config. It's not breaking your app, just breaking the linting.
Upvotes: 1
Reputation: 1224
This looks like a typescript (possibly other linter error). You need to disable the no-undef rule for this line. There is no way the parser can know at design/compile-time that this will be a defined at runtime when the page renders.
Upvotes: 1