简体   繁体   English

在REACT TypeScript中调用第三方函数

[英]Calling a Third Party Function in REACT TypeScript

I want to call WOW when a ReactJS Component has loaded 我想在ReactJS组件加载后调用WOW

The WOW Javascript is referenced at the bottom of the HTML on the inital HTML load. 在初始HTML加载的HTML底部引用了WOW Javascript。

the Javascript way to initiate is JavaScript的初始化方式是

new WOW().init();

and I thought it would be good to call this on "componentDidMount()" 我认为最好在“ componentDidMount()”上调用它

my component code is: 我的组件代码是:

import * as React from 'react'; 

class Banner extends React.Component {
    componentDidMount() {
         new WOW().init();  //This does not work "[ts] Cannot find name "WOW"
    } 
    shouldComponentUpdate() {
        return false;
    }
    render() {
        return (
            <div className="banner-section">
                <div className="an-home-img-container banner-1">
                    <div className="overlay"></div>
                    <div className="home-banner-content text-center">
                        <div className="container">
                            <h1 className="wow fadeInDown animate hidden">
                                Some <span>Text</span> Here
                        </h1>
                            <button className="an-btn an-btn-default btn-big wow fadeIn hidden">Expole Us</button>
                        </div>
                    </div>
                </div>
            </div >
        )
    } }

export default Banner;

How can I call a JS function that isn't imported? 如何调用未导入的JS函数?

You can declare the variable before using it: 您可以在使用变量之前声明它:

declare var WOW: any;

class Banner extends React.Component {
    componentDidMount() {
         new WOW().init();
    } 
    // ...
}

Note that this will type WOW as any so you will lose type checking. 请注意,这将输入WOW作为any WOW ,因此您将丢失类型检查。 If you need type checking you can declare WOW with a type instead of any : 如果需要类型检查,则可以使用类型而不是any类型声明WOW

interface WOW {
    init: () => void
}

interface WOWConstructor {
    new(): WOW
}

declare var WOW: WOWConstructor;

class Banner extends React.Component {
    componentDidMount() {
         new WOW().init();
    } 
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM