Using document.querySelector in React? Should I use refs instead? How?

前端 未结 2 609
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 02:59

I am building a carousel right now, in React. To scroll to the individual slides I am using document.querySelector like so :

useEffect(() => {
           


        
2条回答
  •  孤街浪徒
    2021-02-14 03:34

    I can't answer the "should you" part of whether to use refs for this instead other than if you do, you don't need those id values unless you use them for something else.

    But here's how you would:

    1. Use useRef(null) to create the ref.

      const activeSlideRef = useRef(null);
      
    2. Put it on the Slide that's currently active

      
      
    3. In your useEffect, use the ref's current property

      useEffect(() => {
          if (activeSlideRef.current) {
              activeSlideRef.current.scrollIntoView({
                behavior: 'smooth',
                block: 'nearest',
                inline: 'nearest'
              });
          }
      }, [activeSlide]);
      

      (I think activeSlide is a reasonable dependency for that effect. You can't use the ref, the ref itself doesn't vary...)

    Live example, I've turned some of your components into divs for convenience:

    const {useEffect, useRef, useState} = React;
    
    function Deck({children}) {
        const [activeSlide, setActiveSlide] = useState(0);
        const activeSlideRef = useRef(null);
    
        useEffect(() => {
            if (activeSlideRef.current) {
                activeSlideRef.current.scrollIntoView({
                  behavior: 'smooth',
                  block: 'nearest',
                  inline: 'nearest'
                });
            }
        }, [activeSlide]);
    
        const moveLeft = Math.max(0, activeSlide - 1);
        const moveRight = Math.min(children.length - 1, activeSlide + 1);
    
        return (
            
              
              
    {children.map((child, i) => { const active = i === activeSlide; return (
    {child}
    ); })}
    ); } ReactDOM.render(
    slide 0
    slide 1
    slide 2
    slide 3
    slide 4
    slide 5
    slide 6
    slide 7
    slide 8
    slide 9
    , document.getElementById("root") );
    .slide {
      height: 4em;
      vertical-align: middle;
      text-align: center;
    }
    #test {
      overflow: scroll;
      max-height: 20em;
    }
    .active {
      font-weight: bold;
      color: blue;
    }


    In a comment you've asked:

    Do you know whether it's possible to disable useEffect here for the first render?

    To keep non-state per-component info around, interestingly you use useRef. The docs for useRef point out that it's not just for DOM element references, it's also for per-component non-state data. So you could have

    const firstRenderRef = useRef(true);
    

    then in your useEffect callback, check firstRenderRef.current &mndash; if it's true, set it false, otherwise do the scrolling:

    const {useEffect, useRef, useState} = React;
    
    function Deck({children}) {
        const [activeSlide, setActiveSlide] = useState(0);
        const activeSlideRef = useRef(null);
        // *** Use a ref with the initial value `true`
        const firstRenderRef = useRef(true);
    
        console.log("render");
    
        useEffect(() => {
            // *** After render, don't do anything, just remember we've seen the render
            if (firstRenderRef.current) {
                console.log("set false");
                firstRenderRef.current = false;
            } else if (activeSlideRef.current) {
                console.log("scroll");
                activeSlideRef.current.scrollIntoView({
                  behavior: 'smooth',
                  block: 'nearest',
                  inline: 'nearest'
                });
            }
        }, [activeSlide]);
    
        const moveLeft = Math.max(0, activeSlide - 1);
        const moveRight = Math.min(children.length - 1, activeSlide + 1);
    
        return (
            
              
              
    {children.map((child, i) => { const active = i === activeSlide; return (
    {child}
    ); })}
    ); } ReactDOM.render(
    slide 0
    slide 1
    slide 2
    slide 3
    slide 4
    slide 5
    slide 6
    slide 7
    slide 8
    slide 9
    , document.getElementById("root") );
    .slide {
      height: 4em;
      vertical-align: middle;
      text-align: center;
    }
    #test {
      overflow: scroll;
      max-height: 10em;
    }
    .active {
      font-weight: bold;
      color: blue;
    }


    As a thought experiment, I wrote a hook to make the ergonomics a bit easier:

    function useInstance(instance = {}) {
        // assertion: instance && typeof instance === "object"
        const ref = useRef(instance);
        return ref.current;
    }
    

    Usage:

    const inst = useInstance({first: true});
    

    In useEffect, if inst.first is true, do inst.first = false;; otherwise, do the scrolling.

    Live:

    const {useEffect, useRef, useState} = React;
    
    function useInstance(instance = {}) {
        // assertion: instance && typeof instance === "object"
        const ref = useRef(instance);
        return ref.current;
    }
    
    function Deck({children}) {
        const [activeSlide, setActiveSlide] = useState(0);
        const activeSlideRef = useRef(null);
        const inst = useInstance({first: true});
    
        console.log("render");
    
        useEffect(() => {
            // *** After render, don't do anything, just remember we've seen the render
            if (inst.first) {
                console.log("set false");
                inst.first = false;
            } else if (activeSlideRef.current) {
                console.log("scroll");
                activeSlideRef.current.scrollIntoView({
                  behavior: 'smooth',
                  block: 'nearest',
                  inline: 'nearest'
                });
            }
        }, [activeSlide]);
    
        const moveLeft = Math.max(0, activeSlide - 1);
        const moveRight = Math.min(children.length - 1, activeSlide + 1);
    
        return (
            
              
              
    {children.map((child, i) => { const active = i === activeSlide; return (
    {child}
    ); })}
    ); } ReactDOM.render(
    slide 0
    slide 1
    slide 2
    slide 3
    slide 4
    slide 5
    slide 6
    slide 7
    slide 8
    slide 9
    , document.getElementById("root") );
    .slide {
      height: 4em;
      vertical-align: middle;
      text-align: center;
    }
    #test {
      overflow: scroll;
      max-height: 10em;
    }
    .active {
      font-weight: bold;
      color: blue;
    }

提交回复
热议问题