gpt4 book ai didi

reactjs - React | 如何获取元素的位置Redux (.getBoundingClientRect() + .getWrappedInstance())

转载 作者:行者123 更新时间:2023-12-03 13:37:46 26 4
gpt4 key购买 nike

我正在尝试找到使用 React 渲染的组件的位置。我使用的是 redux,所以我必须将 connect 函数修改为:

export default connect(mapStateToProps, mapDispatchToProps,null,{ withRef: true })(MyComponent);

现在,当我调用时,我会得到一个组件:

class OneColumn extends Component { 
componentDidMount(){

var input = this.refs.myComponentsRef.getWrappedInstance();

console.log(input); // Outputs > TheComponentIWant {props: Object, context: Object, refs: Object, updater: Object, _reactInternalInstance: ReactCompositeComponentWrapper…}

var inputRect = input.getBoundingClientRect();
console.log(inputRect); // Outputs error: Uncaught TypeError: input.getBoundingClientRect is not a function
}
//...
}

所以我可以获取组件,但是我无法获取boundingClientRect。

我忽略了什么吗?

请帮忙:)

最佳答案

我找到了绕过此问题的替代解决方案:This post describes and refers to another post

基本上,当组件开始渲染时,组件会提供一个回调函数,您可以在其中存储这些组件的位置。

关于reactjs - React | 如何获取元素的位置Redux (.getBoundingClientRect() + .getWrappedInstance()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44519452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com