What I want: to get element(<ul>
) scrollwidth inside of the style attribute inside DOM
What I am trying:
The setSliderScrollWidth
in useEffect
should update the state for sliderScrollWidth
, the sliderEl.current.scrollWidth
value is avaiable inside of the useEffect, but still inside of useEffect the sliderScrollWidth
is 0.
Also I can not get sliderEl.current.scrollWidth
inside of the DOM, so I am unable to get the inside of DOM with either useState or useRef.
const TextSlider = () => {
const sliderEl = useRef(null);
const [sliderScrollWidth, setSliderScrollWidth] = useState(0);
useEffect(() => {
setSliderScrollWidth(sliderEl.current.scrollWidth);
console.log(sliderScrollWidth, sliderEl.current.scrollWidth);
},[])
return(
<ul
ref={sliderEl}
style={{
width: `${sliderEl.current.scrollWidth}`,
}}
>
<li>Hello</li>
</ul>
)
}
What I don't understand: 1) Why if I can get sliderEl.current.scrollWidth
inside of useEffect
the sliderScrollWidth
value is still 0? 2) Why can't I get the ref.current value inside the DOM?