【发布时间】:2019-04-06 19:26:24
【问题描述】:
如何使用 Typescript 获得 React children offsetTop?
这是我的组件:
export default class FadeIn extends Component {
private onScroll = () => {
React.Children.forEach(this.props.children, child => {
// Get the child's offsetTop here and do stuff with
})
}
public componentDidMount() {
window.addEventListener('scroll', this.onScroll)
}
public componentWillUnmount() {
window.removeEventListener('scroll', this.onScroll)
}
public render() {
return this.props.children
}
}
我已经尝试过:
- 使用
ReactDOM.findDOMNode(child)但我得到Argument of type 'ReactChild' is not assignable to parameter of type 'ReactInstance'. Type 'string' is not assignable to type 'ReactInstance' - 使用
getBoundingClientRect但我得到Property 'getBoundingClientRect' does not exist on type 'ReactChild'. Property 'getBoundingClientRect' does not exist on type 'string' - 将孩子投给
ReactElement<any>
有什么想法吗?
【问题讨论】:
标签: javascript reactjs typescript dom typescript-typings