简体   繁体   English

如何将外部库RainyDay.js添加到我的项目中?

[英]How can I add the external library RainyDay.js to my project?

So I'm quite new with React, I just create a project, define a background image and I was wondering how could I use the library https://mubaidr.js.org/rainyday.js/ ? 所以我对React还是很陌生,我只是创建一个项目,定义背景图片,我想知道如何使用库https://mubaidr.js.org/rainyday.js/ How to add it and what are the good pratice when it comes to add external library in react. 在react中添加外部库时,如何添加它和有什么好的习惯?

Thanks in advance ! 提前致谢 !

This project is not available on npm, so you can't install it like you are used to with other modules. 该项目在npm上不可用,因此无法像使用其他模块一样安装它。 You can include it in a script tag, or copy the source to your own project and include it that way. 您可以将其包含在script标签中,或将源复制到您自己的项目中并以这种方式包含它。

To use it you can put a ref on your image, and create the RainyDay object with the image as the image option in the componentDidMount lifecycle hook. 要使用它,您可以在图像上放置一个ref ,并在componentDidMount生命周期挂钩中创建将RainyDay对象作为图像的image选项。

Example

class App extends React.Component {
  ref = React.createRef();

  componentDidMount() {
    const image = this.ref.current;

    image.onload = function() {
      new RainyDay({
        image
      });
    };

    image.crossOrigin = "anonymous";
    image.src = "https://picsum.photos/200";
  }

  render() {
    return <img ref={this.ref} src="" />;
  }
}

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

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