如何在React组件中使用JavaScript

时间:2018-11-20 23:37:36

标签: javascript reactjs components

我正在使用React组件,我发现了一个特定的javascript代码,可以修改我的html页面的内容。 问题是我不知道如何将此脚本合并到组件中。我知道我可以使用“ import”语句来调用它,但是脚本可以与“ window.onload”函数一起使用,该函数只能被调用一次,但是我的组件已挂载了几次,脚本不再起作用

组件-

import React, { Component } from 'react';
import textRotation from '../../scripts/textRotation';
import './Main.scss';


class Main extends Component {
  constructor(props) {
    super(props);
    this.title = props.title;
    this.imgSrc = props.imgSrc;
  }

  render() {
    return (
      <div className="Main">
        <div className="main-content">
          <div className="presentation-caption">
            <span>Hello, I'm Jordan, a junior front-end developer, who does a lot of things.</span>
          </div>
          <div className="description-caption">
            <span>Prepare you to encounter various types of projects... but it's ok, just explore&nbsp;!</span>
          </div>
          <div className="button-container">
            <a href="#works"><button>Scroll down</button></a>
          </div>
        </div>
        <div className="side-content">
          <span
            className="txt-rotate"
            data-period="1100"
            data-rotate='[ "Web development", "Video editing", "Motion design", "Graphism", "Creativity" ]'></span><span>.</span>
        </div>
        <div className="side-text-portfolio">
          <span>ポ<br />ー<br />ト<br />フ<br />ォ<br />リ<br />オ</span>
        </div>
      </div>
    );
  }
}

export default Main;

脚本-

var TxtRotate = function (el, toRotate, period) {
  this.toRotate = toRotate;
  this.el = el;
  this.loopNum = 0;
  this.period = parseInt(period, 10) || 2000;
  this.txt = '';
  this.tick();
  this.isDeleting = false;
};

TxtRotate.prototype.tick = function () {
  var i = this.loopNum % this.toRotate.length;
  var fullTxt = this.toRotate[i];

  if (this.isDeleting) {
    this.txt = fullTxt.substring(0, this.txt.length - 1);
  } else {
    this.txt = fullTxt.substring(0, this.txt.length + 1);
  }

  this.el.innerHTML = '<span class="wrap">' + this.txt + '</span>';

  var that = this;
  var delta = 300 - Math.random() * 100;

  if (this.isDeleting) { delta /= 2; }

  if (!this.isDeleting && this.txt === fullTxt) {
    delta = this.period;
    this.isDeleting = true;
  } else if (this.isDeleting && this.txt === '') {
    this.isDeleting = false;
    this.loopNum++;
    delta = 500;
  }

  setTimeout(function () {
    that.tick();
  }, delta);
};

window.onload = function () {
  var elements = document.getElementsByClassName('txt-rotate');
  for (var i = 0; i < elements.length; i++) {
    var toRotate = elements[i].getAttribute('data-rotate');
    var period = elements[i].getAttribute('data-period');
    if (toRotate) {
      new TxtRotate(elements[i], JSON.parse(toRotate), period);
    }
  }
  // INJECT CSS
  var css = document.createElement("style");
  css.type = "text/css";
  css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #725070 }";
  document.body.appendChild(css);
};

如果您有将脚本添加到我的组件中的任何解决方案,或者可以正确地重新加载它...感谢您提前

2 个答案:

答案 0 :(得分:0)

如果您能够更改脚本中的代码,则可以将其更改为可调用的,而不是在加载时触发,即为函数命名。然后,您可以在组件的componentDidMount挂钩中调用该函数。

答案 1 :(得分:0)

将脚本更改为此:

var TxtRotate = function (el, toRotate, period) {
  this.toRotate = toRotate;
  this.el = el;
  this.loopNum = 0;
  this.period = parseInt(period, 10) || 2000;
  this.txt = '';
  this.tick();
  this.isDeleting = false;
};

TxtRotate.prototype.tick = function () {
  var i = this.loopNum % this.toRotate.length;
  var fullTxt = this.toRotate[i];

  if (this.isDeleting) {
    this.txt = fullTxt.substring(0, this.txt.length - 1);
  } else {
    this.txt = fullTxt.substring(0, this.txt.length + 1);
  }

  this.el.innerHTML = '<span class="wrap">' + this.txt + '</span>';

  var that = this;
  var delta = 300 - Math.random() * 100;

  if (this.isDeleting) { delta /= 2; }

  if (!this.isDeleting && this.txt === fullTxt) {
    delta = this.period;
    this.isDeleting = true;
  } else if (this.isDeleting && this.txt === '') {
    this.isDeleting = false;
    this.loopNum++;
    delta = 500;
  }

  setTimeout(function () {
    that.tick();
  }, delta);
};

loadCall = function () {
  var elements = document.getElementsByClassName('txt-rotate');
  for (var i = 0; i < elements.length; i++) {
    var toRotate = elements[i].getAttribute('data-rotate');
    var period = elements[i].getAttribute('data-period');
    if (toRotate) {
      new TxtRotate(elements[i], JSON.parse(toRotate), period);
    }
  }
  // INJECT CSS
  var css = document.createElement("style");
  css.type = "text/css";
  css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #725070 }";
  document.body.appendChild(css);
};

const loadMyScript = () => window.addEventListener('load', () => loadCall());

export default loadMyScript;

然后导入loadMyScript并从组件中的loadMyScript()函数调用componentDidMount()