How to get the width of a react element

前端 未结 8 1100
灰色年华
灰色年华 2020-12-01 01:10

Im trying to create a range input that displays a tooltip right above the slider thumb.

I went through some vanilla JS examples online and it seems that I need to ha

相关标签:
8条回答
  • 2020-12-01 01:33

    This could be handled perhaps in a simpler way by using callback refs.

    React allows you to pass a function into a ref, which returns the underlying DOM element or component node. See: https://reactjs.org/docs/refs-and-the-dom.html#callback-refs

    const MyComponent = () => {
        const myRef = node => console.log(node ? node.innerText : 'NULL!');
        return <div ref={myRef}>Hello World</div>;
     }
    

    This function gets fired whenever the underlying node is changed. It will be null in-between updates, so we need to check for this. Example:

    const MyComponent = () => {
        const [time, setTime] = React.useState(123);
        const myRef = node => console.log(node ? node.innerText : 'NULL!');
        setTimeout(() => setTime(time+1), 1000);
        return <div ref={myRef}>Hello World {time}</div>;
    }
    /*** Console output: 
     Hello World 123
     NULL!
     Hello World 124
     NULL!
    ...etc
    ***/
    

    While this does't handle resizing as such (we would still need a resize listener to handle the user resizing the window) I'm not sure that is what the OP was asking for. And this version will handle the node resizing due to an update.

    So here is a custom hook based on this idea:

    export const useClientRect = () => {
        const [rect, setRect] = useState({width:0, height:0});
        const ref = useCallback(node => {
            if (node !== null) {
                const { width, height } = node.getBoundingClientRect();
                setRect({ width, height });
            }
        }, []);
        return [rect, ref];
    };
    

    The above is based on https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node

    Note the hook returns a ref callback, instead of being passed a ref. And we employ useCallback to avoid re-creating a new ref function each time; not vital, but considered good practice.

    Usage is like this (based on Marco Antônio's example):

    const MyComponent = ({children}) => {
      const [rect, myRef] = useClientRect();
      const { width, height } = rect;
    
      return (
        <div ref={myRef}>
          <p>width: {width}px</p>
          <p>height: {height}px</p>
          {children}
        <div/>
      )
    }
    
    0 讨论(0)
  • 2020-12-01 01:37

    Here is a TypeScript version of @meseern's answer that avoids unnecessary assignments on re-render:

    import React, { useState, useEffect } from 'react';
    
    export function useContainerDimensions(myRef: React.RefObject<any>) {
      const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
    
      useEffect(() => {
        const getDimensions = () => ({
          width: (myRef && myRef.current.offsetWidth) || 0,
          height: (myRef && myRef.current.offsetHeight) || 0,
        });
    
        const handleResize = () => {
          setDimensions(getDimensions());
        };
    
        if (myRef.current) {
          setDimensions(getDimensions());
        }
    
        window.addEventListener('resize', handleResize);
    
        return () => {
          window.removeEventListener('resize', handleResize);
        };
      }, [myRef]);
    
      return dimensions;
    }
    
    0 讨论(0)
  • 2020-12-01 01:39

    With hooks:

    const MyComponent = () => {
      const ref = useRef(null);
      useEffect(() => {
        console.log('width', ref.current ? ref.current.offsetWidth : 0);
      }, [ref.current]);
      return <div ref={ref}>Hello</div>;
    };
    
    0 讨论(0)
  • 2020-12-01 01:45

    A simple and up to date solution is to use the React React useRef hook that stores a reference to the component/element, combined with a useEffect hook, which fires at component renders.

    import React, {useState, useEffect, useRef} from 'react';
    
    export default App = () => {
      const [width, setWidth] = useState(0);
      const elementRef = useRef(null);
    
      useEffect(() => {
        setWidth(elementRef.current.getBoundingClientRect().width);
      }, []); //empty dependency array so it only runs once at render
    
      return (
        <div ref={elementRef}>
          {width}
        </div>
      )
    }
    
    0 讨论(0)
  • 2020-12-01 01:46

    Actually, would be better to isolate this resize logic in a custom hook. You can create a custom hook like this:

    const useResize = (myRef) => {
      const [width, setWidth] = useState(0)
      const [height, setHeight] = useState(0)
    
      useEffect(() => {
        const handleResize = () => {
          setWidth(myRef.current.offsetWidth)
          setHeight(myRef.current.offsetHeight)
        }
    
        window.addEventListener('resize', handleResize)
    
        return () => {
          window.removeEventListener('resize', handleResize)
        }
      }, [myRef])
    
      return { width, height }
    }
    

    and then you can use it like:

    const MyComponent = () => {
      const componentRef = useRef()
      const { width, height } = useResize(componentRef)
    
      return (
        <div ref={myRef}>
          <p>width: {width}px</p>
          <p>height: {height}px</p>
        <div/>
      )
    }
    
    0 讨论(0)
  • 2020-12-01 01:57

    A good practice is listening for resize events to prevent resize on render or even a user window resize that can bug your application.

    const MyComponent = ()=> {
      const myRef = useRef(null)
    
      const [myComponenetWidth, setMyComponentWidth] = useState('')
    
      const handleResize = ()=>{ 
        setMyComponentWidth(myRef.current.offsetWidth)
      }
    
      useEffect(() =>{
        if(myRef.current)
          myRef.current.addEventListener('resize', handleResize)
    
        return ()=> {
         myRef.current.removeEventListener('resize', handleResize)
        }
      }, [myRef])
    
      return (
      <div ref={MyRef}>Hello</div>
      )
    }
    
    0 讨论(0)
提交回复
热议问题