我想在搜索组件中进行特定的计算。为此,我必须将在 Header 组件中获得的 buttonSearchRefOffsetLeftValue 传递给 Search 组件。我在标题中获得了所需的值。但是,在 Search 组件中,它总是未定义。无法理解这种行为。
const Header = props => {
const buttonSearchRef = useRef(null);
let buttonSearchRefOffsetLeftValue;
useEffect(() => {
buttonSearchRefOffsetLeftValue =
ReactDOM.findDOMNode(buttonSearchRef.current).offsetLeft;
console.log(buttonSearchRefOffsetLeftValue); //got the value
})
return (
<div ref={buttonSearchRef}>He<div>
<Search buttonSearchRefOffsetLeftValue=
{buttonSearchRefOffsetLeftValue} />
)
}
const Search = (props) => {
console.log(props.buttonSearchRefOffsetLeftValue)//got undefined WHY?
}