Wie kann man einfaches (Vanille-) JavaScript in ReactJS integrieren?

Oct 01 2020

Ich versuche, diese 3D-Gio-Visualisierung in meiner ReactJs-Anwendung zu verwenden. Der Code ist in Vanille-JavaScript geschrieben. Ich habe auch einen Wrapper für React gefunden und ausprobiert, kann aber das ausgewählte Land ( onCountryPicked()) nicht auswählen, wie es in der Demo gezeigt wird . Dies ist der Hauptgrund, warum ich die Vanille-JavaScript-Implementierung anstelle des Wrappers verwende. Ich finde es jedoch schwierig, die Vanilla-JavaScript-Implementierung in meine ReactJs-Anwendung zu integrieren.

Ich habe mich umgesehen, um das Problem zu lösen, aber die Ressourcen, die ich gefunden habe, haben mir nicht geholfen. Das Gebrüll sind einige der Orte, die ich besucht habe.

  • Wie verwende ich Vanille-Javascript in der Reactjs-Komponente?
  • https://reactjs.org/docs/integrating-with-other-libraries.html

helloworld.js

var container = document.getElementById("globalArea");

var controller = new GIO.Controller(container, {
  color: {...},
  brightness: {...},
});

controller.onCountryPicked(callback);
controller.setInitCountry("FR");
controller.showOutOnly(false);
controller.showInOnly(false);

function callback(selectedCountry) {
  $("#countryArea").text(selectedCountry.name + " picked!"); $("#infoBoard").fadeIn(300);
  setTimeout(function () {
    $("#infoBoard").fadeOut(1000); }, 3000); } axios .get("./test_data.json") .then(function (response) { controller.addData(response.data); }) .catch(function (error) { console.log(error); }) .then(function () { controller.init(); }); var d3Graphs = { tiltBtnInterval: -1, }; function showHud() { $("#hudButtons").show();
  $(".tiltBtn").on("mousedown touchstart", d3Graphs.tiltBtnClick); $(".tiltBtn").on("mouseup touchend touchcancel", d3Graphs.tiltBtnMouseup);
}

function tiltBtnClick() {
  var delta;
  if ($(this).hasClass("sideViewBtn")) {
    delta = 10;
  } else {
    delta = -10;
  }
  d3Graphs.doTilt(delta);
  d3Graphs.tiltBtnInterval = setInterval(d3Graphs.doTilt, 50, delta);
}

function doTilt(delta) {
  tilt += delta * 0.01;
  tilt = constrain(tilt, 0, Math.PI / 2);
  camera.position.y = 300 * Math.sin(-tilt);
  camera.position.z = 100 + 300 * Math.cos(-tilt);
  camera.lookAt(new THREE.Vector3(0, 0, 300));
  tiltTarget = undefined;
}

............

home.html

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <script src="./lib/jquery-3.3.1.min.js"></script>
    <script src="./lib/three.min.js"></script>
    <script src="./lib/gio.min.js"></script>
    <script src="./lib/axios.min.js"></script>
    <script src="https://d3js.org/d3.v5.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
    <link rel="stylesheet" href="helloworld.css" />
  </head>

  <body>
    <div class="b">
      <div id="infoBoard">
        <div id="countryArea"></div>
        <div id="explanation">Infos here</div>
      </div>
      <script src="helloworld.js"></script>
    </div>
  </body>
</html>

................

Sample.js

export class Sample extends React.Component {
  componentDidMount() {
  
  }

  render() {
    return (
      <div className="App">
         ...
      </div>
    );
  }
}

Antworten

2 LionelRowe Oct 04 2020 at 18:50

Hier ist ein Ausgangspunkt. Ich habe die Teile kommentiert, in denen React mit Gio.js interagiert.

const Globe = () => {
  // use a React ref whenever you need to directly target a DOM element
  // - this is required as an argument to the GIO.Controller constructor
  const ref = useRef(null);
  const [country, setCountry] = useState(initCountry);

  useEffect(() => {
    // useEffect with empty dependency array
    // - this will run once when the component mounts
    const controller = new GIO.Controller(ref.current, {
      control: {
        initCountry
      }
    });

    // import and add data here if you want the glowing lines
    controller.addData([]);

    controller.init();

    // here's the callback for when the user clicks a country
    // we can use the React setState hook inside the callback
    controller.onCountryPicked((country) => {
      setCountry(country.ISOCode);
    });
  }, []);

  return (
    <>
      <div>
        <strong>Selected country: {displayName.of(country)}</strong>
      </div>
      <div style={{ height: 500 }} ref={ref}></div>
    </>
  );
};

CodeSandbox-Demo

PsiKai Oct 01 2020 at 17:56

Wenn Sie Ihre Vanille-Javascript-Datei auf dieselbe Hierarchieebene wie Ihre legen index.htmlund sie normal verknüpfen, sollten Sie in der Lage sein, Ihr Vanille-Javascript zu implementieren, ohne Ihr Reaktions-Javascript zu beeinträchtigen. Verknüpfen Sie es einfach wie gewohnt unten mit einem <script>Tag.

Alternativ können useStateSie in Ihrem Reaktionscode Folgendes verwenden:

import React, {useState} from "react";
import ReactDOM from "react-dom";

const Country = () => {
  const [country, setCountry] = useState('');

  ///your javascript here

  setCountry(//return from javascript function// );

  return (
     <h1>{country}</h1>
  )
}

ReactDOM.render(<Country/>, document.getElementById("countryArea"));

Sie müssen alle zusätzlichen Abhängigkeiten für Ihr Vanille-Javascript (z. B. axios) in diese Datei importieren .