我给我的div增加了一个高度,就像这样:
const parentRef = useRef();
useEffect(() => {
let collapseHeight = parentRef.current.scrollHeight
const collapse = document.querySelector('.collapse-content-parent')
collapse.style.height = collapseHeight + "px"
}, []); 当同一组件有多个实例时,这只会选择页面上的第一个div。
我想做一些事情,比如将querySelector替换为我的参考文献,这样它就特别适用于组件:
const parentRef = useRef();
useEffect(() => {
let collapseHeight = parentRef.current.scrollHeight
parentRef.style.height = collapseHeight + "px"
}, []); 但上述各点如下:
TypeError: Cannot set properties of undefined (setting 'height')
发布于 2021-09-18 11:37:11
不要使用parentRef.style.height,而要使用此parentRef.current.style.height。
https://stackoverflow.com/questions/69234217
复制相似问题