我如何获得React中的QuerySelectorAll HTML元素?

发布于 2025-01-30 19:17:07 字数 3141 浏览 5 评论 0原文

我正在使用功能组件的反应。我还使用了React-Bootstrap,我附上了React-Bootstrap旋转木马,React-Bootstrap一切都很好,对于某些功能,我想在React中获得三个Divs,Divs具有相同的类。我知道,如果我使用简单的HTML CSS JS,那么我很容易做到这一点,

querySelectorAll(“。inideator-btn”)

但我希望在React上进行此操作。我该怎么做?请建议我为此做一个最佳实践。

这是我的组成部分,最后我有三个Divs,我想要DOM。

import React, { useState } from "react";
import Cardcarousel from "./Cardcarousel";
import { Carousel } from "react-bootstrap";
import Brand1img from "../images/brand1.png";
import Airbnbimg from "../images/Airbnb.png";
import Microsoft from "../images/microsoft.png";
import { Row, Col } from "react-bootstrap";

function Carouselx() {
  const [index, setIndex] = useState(0);

  const handleSelect = (selectedIndex, e) => {
    console.log(selectedIndex, "e=>", e);
    setIndex(selectedIndex);
  };

  const isSlide = (slide) => {
    console.log("slide no => " + slide);
    console.log(this);
  };

  return (
    <>
      <div className="px-5 py-3">
        <Carousel activeIndex={index} onSelect={handleSelect} onSlide={isSlide}>
          <Carousel.Item>
            <Row>
              <Col>
                <Cardcarousel
                  name="Technology UI/UX"
                  companylogo={Brand1img}
                  type="Full time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology Backend developer"
                  companylogo={Airbnbimg}
                  type="Part time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology Backend developer"
                  companylogo={Microsoft}
                  type="Internee"
                />
              </Col>
            </Row>
          </Carousel.Item>
          <Carousel.Item>
            <Row>
              <Col>
                <Cardcarousel
                  name="Technology UI/UX"
                  companylogo={Brand1img}
                  type="Full time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology Backend developer"
                  companylogo={Airbnbimg}
                  type="Part time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology front end developer"
                  companylogo={Microsoft}
                  type="Internee"
                />
              </Col>
            </Row>
          </Carousel.Item>
        </Carousel>
        <div className="carusel-indecator-container">
          <div className="indicator-btn indicator-btn1"></div>
          <div className="indicator-btn indicator-btn2"></div>
          <div className="indicator-btn indicator-btn3"></div>
        </div>
      </div>
    </>
  );
}

export default Carouselx;

I am using react with functional component. I also use react-bootstrap , I attached the react-bootstrap carousel , everything is fine with react-bootstrap, for some functionality, I want to get three divs in react , divs has same classes. I know if I use simple html css js then I easily do that,

querySelectorAll(".indicator-btn")

but i want this on react. How can I do that? please suggest me a best practices for that.

This is a my component , in the last i have three divs which i want for Dom.

import React, { useState } from "react";
import Cardcarousel from "./Cardcarousel";
import { Carousel } from "react-bootstrap";
import Brand1img from "../images/brand1.png";
import Airbnbimg from "../images/Airbnb.png";
import Microsoft from "../images/microsoft.png";
import { Row, Col } from "react-bootstrap";

function Carouselx() {
  const [index, setIndex] = useState(0);

  const handleSelect = (selectedIndex, e) => {
    console.log(selectedIndex, "e=>", e);
    setIndex(selectedIndex);
  };

  const isSlide = (slide) => {
    console.log("slide no => " + slide);
    console.log(this);
  };

  return (
    <>
      <div className="px-5 py-3">
        <Carousel activeIndex={index} onSelect={handleSelect} onSlide={isSlide}>
          <Carousel.Item>
            <Row>
              <Col>
                <Cardcarousel
                  name="Technology UI/UX"
                  companylogo={Brand1img}
                  type="Full time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology Backend developer"
                  companylogo={Airbnbimg}
                  type="Part time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology Backend developer"
                  companylogo={Microsoft}
                  type="Internee"
                />
              </Col>
            </Row>
          </Carousel.Item>
          <Carousel.Item>
            <Row>
              <Col>
                <Cardcarousel
                  name="Technology UI/UX"
                  companylogo={Brand1img}
                  type="Full time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology Backend developer"
                  companylogo={Airbnbimg}
                  type="Part time job"
                />
              </Col>
              <Col>
                <Cardcarousel
                  name="Technology front end developer"
                  companylogo={Microsoft}
                  type="Internee"
                />
              </Col>
            </Row>
          </Carousel.Item>
        </Carousel>
        <div className="carusel-indecator-container">
          <div className="indicator-btn indicator-btn1"></div>
          <div className="indicator-btn indicator-btn2"></div>
          <div className="indicator-btn indicator-btn3"></div>
        </div>
      </div>
    </>
  );
}

export default Carouselx;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2025-02-06 19:17:07

您可以使用usestate获取其Refs(useref,但可能会带来更多不便)。

const [btn1, setBtn1] = useState();
const [btn2, setBtn2] = useState();
const [btn3, setBtn3] = useState();

...

<div ref={setBtn1} className="indicator-btn indicator-btn1"></div>
<div ref={setBtn2} className="indicator-btn indicator-btn2"></div>
<div ref={setBtn3} className="indicator-btn indicator-btn3"></div>

在某个时候(第一个渲染之后)您将拥有BTN1,BTN2,BTN3作为按钮元素。如果您想使用它

useEffect(() => {
  if (!btn1 || !btn2 || !btn3) return;
  // do something with these refs i.e. make them jquery! $(btn1)
}, [btn1, btn2, btn3]);

You could use useState to get its refs (also useRef but there'll be a lot more inconveniences potentially).

const [btn1, setBtn1] = useState();
const [btn2, setBtn2] = useState();
const [btn3, setBtn3] = useState();

...

<div ref={setBtn1} className="indicator-btn indicator-btn1"></div>
<div ref={setBtn2} className="indicator-btn indicator-btn2"></div>
<div ref={setBtn3} className="indicator-btn indicator-btn3"></div>

At some point (after 1st rendering) you'll have btn1, btn2, btn3 as button elements. If you want to use it, assumingly imperatively, you could useEffect

useEffect(() => {
  if (!btn1 || !btn2 || !btn3) return;
  // do something with these refs i.e. make them jquery! $(btn1)
}, [btn1, btn2, btn3]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文