1
function Slider() {
  const track=document.querySelector('.slide__track')//To access the div with class slide track
  console.log(track);
  return (
    <div className="slider">
      <i className="fas fa-chevron-left"></i>
      <div className="head">
        <h1 className="title">Based on your last search</h1>
        <h6>View more</h6>
      </div>
      <div className="slider_container">
        <ul className="slider__track">
          <li className="slider__items">
            <Card />
          </li>
        </ul>
      </div>
      <i className="fas fa-chevron-right"></i>
    </div>
  );
}

i cannot access the div with class slide__track. What is the problem here? Or how can i access that element?

1
  • Notice that apart from the answers below you also use different names on the css-selector, slide__track vs slider__track Commented Jun 7, 2021 at 11:20

4 Answers 4

6

I think it's because it is running before the DOM has been rendered. Move the track code into useEffect.

import {useEffect} from "react";

function Slider() {

  useEffect(() => {
    const track=document.querySelector('.slide__track')//To access the div with class slide track
    console.log(track);
  });

  return (
    <div className="slider">
      <i className="fas fa-chevron-left"></i>
      <div className="head">
        <h1 className="title">Based on your last search</h1>
        <h6>View more</h6>
      </div>
      <div className="slider_container">
        <ul className="slider__track">
          <li className="slider__items">
            <Card />
          </li>
        </ul>
      </div>
      <i className="fas fa-chevron-right"></i>
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Did you also fix the part @row wrote in his comment? The class names are mismatched slider__track vs slide__track
4

Try and use this code in useEffect()

useEffect(() => {
    const track = document.querySelector('.slide__track')
    // have access to it
}, []);

1 Comment

Elaborate more.
1

You should be probably be using ref's to access dom elements inside react. see docs: https://reactjs.org/docs/refs-and-the-dom.html - reason being, if you have the below in a loop, or have multiple instances on the page, you'll need to be more careful with the selector approach.

import React, { useRef, useEffect } from 'react';

function Slider() {


  const slideTrackRef = useRef(null);

  useEffect(() => {
    if (slideTrackRef && slideTrackRef.current) {
      console.log(slideTrackRef.current);
    }
  }, [slideTrackRef]);

  return (
    <div className="slider">
      <i className="fas fa-chevron-left" />
      <div className="head">
        <h1 className="title">Based on your last search</h1>
        <h6>View more</h6>
      </div>
      <div className="slider_container">
        <ul className="slider__track" ref={slideTrackRef}>
          <li className="slider__items">
            <p>tst</p>
          </li>
        </ul>
      </div>
      <i className="fas fa-chevron-right" />
    </div>
  );
}

export default Slider;

Comments

0

For some reaseon .querySelector("#myId1") didn't work for me but .querySelector("[id="#myId1"]")

Comments

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.