Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useOnScreenCallback 返回的 ref 不能获取 ref.current 么 #70

Closed
feibinyang opened this issue Jan 6, 2021 · 2 comments
Closed

Comments

@feibinyang
Copy link
Member

问题:使用 useOnScreenCallback 这个 hook 返回的 ref,赋值给 DOM 元素后,ref.current 不能获取到 DOM。但在使用中会有需要获取这个 DOM 进行操作的情景(比如 add/remove classList 来显示动画);

期望:希望赋值完 ref={ref} 后,ref.current 可以获取 DOM 结构,这样可以在 callback 里直接进行后续操作。

参考 demo:https://codesandbox.io/s/heuristic-haslett-s1qs8?fontsize=14&hidenavigation=1&theme=dark

@feibinyang
Copy link
Member Author

忘记有 entry.target 了

@otakustay
Copy link
Member

顺便提一下,类似其它需要这种功能的场景,也可以这样:

const elementRef = useRef(null);
const registerOnScreen = useOnScreenCallback(...);
const ref = useMergedRef([elementRef, registerOnScreen]);

<div ref={ref} />

参考:https://ecomfe.github.io/react-hooks/#/hook/merged-ref/use-merged-ref

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants