4

I'm working on my first ever React app and trying to get my head around how I am meant to use small JS snippets in the page.

For example; I want to use the following Interactive SVG Chris Coyer created in my code. Adding HTML and CSS is easy but what should be the correct way of implementing the JS?

Copy and pasting into my home.js page clearly won't work.

Interactive SVG - Demo

var wordStates = document.querySelectorAll(".list-of-states li");
var svgStates = document.querySelectorAll("#states > *");

function removeAllOn() {
  wordStates.forEach(function(el) {
    el.classList.remove("on");
  });
  svgStates.forEach(function(el) {
    el.classList.remove("on");
  });
}

function addOnFromList(el) {
  var stateCode = el.getAttribute("data-state");
  var svgState = document.querySelector("#" + stateCode);
  el.classList.add("on");
  svgState.classList.add("on");
}

function addOnFromState(el) {
  var stateId = el.getAttribute("id");
  var wordState = document.querySelector("[data-state='" + stateId + "']");
  el.classList.add("on");
  wordState.classList.add("on");
}

wordStates.forEach(function(el) {
  el.addEventListener("mouseenter", function() {
    addOnFromList(el);
  });
  el.addEventListener("mouseleave", function() {
     removeAllOn();
  });

  el.addEventListener("touchstart", function() {
    removeAllOn();
    addOnFromList(el);
  });
});

svgStates.forEach(function(el) {
  el.addEventListener("mouseenter", function() {
    addOnFromState(el);
  });
  el.addEventListener("mouseleave", function() {
     removeAllOn();
  });

  el.addEventListener("touchstart", function() {
    removeAllOn();
    addOnFromState(el);
  });
});
3
  • Have you tried anything yet? Commented Jun 4, 2018 at 7:48
  • Is there a certain reason you want this in react? If you wanna build it "the React way" then you will need to break the pieces down to components. I would break this into 3 components (App, Map and List) where Map and List share props from the App which holds the state for the hovering events and such. Commented Jun 4, 2018 at 7:55
  • No I haven't tried anything yet. This is a question clarifying execution - what am I meant to do? I noticed the title was change to "react component class format". So I'm guessing I need to create a component class? Commented Jun 4, 2018 at 9:55

1 Answer 1

2

You can use it by adding the wordStates and svgStates as class variables and manipulating those in component by adding the query selector functions into componentdid mount function like,

import * as React from 'react';
import { Component } from "react";
class Home extends Component {

 constructor(props) {
  super(props); 
  this.wordStates=[];
  this.svgStates=[]; 
  } 


  removeAllOn =()=> {
    this.wordStates.forEach(function (el) {
      el.classList.remove("on");
    });
    this.svgStates.forEach(function (el) {
      el.classList.remove("on");
    });
  }

  addOnFromList=(el)=> {
    var stateCode = el.getAttribute("data-state");
    var svgState = document.querySelector("#" + stateCode);
    el.classList.add("on");
    svgState.classList.add("on");
  }

  addOnFromState=(el)=> {
    var stateId = el.getAttribute("id");
    var wordState = document.querySelector("[data-state='" + stateId + "']");
    el.classList.add("on");
    wordState.classList.add("on");
  }
  componentDidMount() {
    this.wordStates = document.querySelectorAll(".list-of-states li");
    this.svgStates = document.querySelectorAll("#states > *");
    this.wordStates.forEach(function (el) {
      el.addEventListener("mouseenter", function () {
        addOnFromList(el);
      });
      el.addEventListener("mouseleave", function () {
        removeAllOn();
      });

      el.addEventListener("touchstart", function () {
        removeAllOn();
        addOnFromList(el);
      });
    });

    this.svgStates.forEach(function (el) {
      el.addEventListener("mouseenter", function () {
        addOnFromState(el);
      });
      el.addEventListener("mouseleave", function () {
        removeAllOn();
      });

      el.addEventListener("touchstart", function () {
        removeAllOn();
        addOnFromState(el);
      });
    });
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi I get the error: Failed to compile ./src/app/Home.js Syntax error: Unexpected token (28:10) > private wordStates; This error occurred during the build time and cannot be dismissed.
I have edited the code to intialise the private variables to empty array, pls make this change and retest
I'm afraid I'm getting the same error. Syntax error: Unexpected token class Home extends Component {private wordStates=[];
Oops, that private specifiers are not supposed to be there. Sorry. I have been using react in typescript, hence I didnt notice those lying around. :) Pls check now
Made a small change again to add constructor It will be more proper to intialise the variable inside the constructor.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.