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

useImperativeHandle 如何使用 #5376

Closed
Zigei opened this issue Jan 20, 2020 · 7 comments
Closed

useImperativeHandle 如何使用 #5376

Zigei opened this issue Jan 20, 2020 · 7 comments

Comments

@Zigei
Copy link

Zigei commented Jan 20, 2020

问题描述

'@tarojs/taro' 中含有 useImperativeHandle 方法 但是 d.ts 的描述文件里面写的是 react 的 无法正常在 hooks 中使用
hooks 无法传入 第二个参数 ref
目前要如何 才能在 hooks 中使用 ref 暴露出去 这个组件

期望行为

添加 正确的 useImperativeHandle 使用说明 或者更新 d.ts 文件

系统信息

Taro CLI 2.0.1 environment info:
System:
OS: macOS 10.15.2
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 13.2.0 - /usr/local/bin/node
Yarn: 1.21.1 - ~/.yarn/bin/yarn
npm: 6.13.1 - /usr/local/bin/npm
npmGlobalPackages:
typescript: 3.7.4

@taro-bot
Copy link

taro-bot bot commented Jan 20, 2020

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@luckyadam
Copy link
Member

暴露出的 hooks api 与 typing 是对得上的啊

@Zigei
Copy link
Author

Zigei commented Jan 21, 2020

暴露出的 hooks api 与 typing 是对得上的啊

useImperativeHandle 需要配合 forwardRef 源代码里面 这个 关键字 但是并没有导出

@z724211471
Copy link

z724211471 commented Feb 20, 2020

可以这样使用
function Father(){ const childRef=useRef() function click(){ chatroomRef.current.handless() } return (<Child childRef={childRef}></Child>) }

function Child({childRef}){ useImperativeHandle(childRef, () => ({ handless: () => { } })) return (<View>ss</View>) }

@Chen-jj Chen-jj closed this as completed Jul 2, 2020
@yz1311
Copy link

yz1311 commented Sep 18, 2020

@luckyadam @Chen-jj 能不能说明下为啥没有导出这个api?

@xchunzhao
Copy link

可以这样使用
function Father(){ const childRef=useRef() function click(){ chatroomRef.current.handless() } return (<Child childRef={childRef}></Child>) }

function Child({childRef}){ useImperativeHandle(childRef, () => ({ handless: () => { } })) return (<View>ss</View>) }

Taro 2.0.7这样使用也不行

@liufeisprit
Copy link

taro 2到底父组件 怎么调用子组件方法呢? useImperativeHandle forwardRef 又是阉割版的 唉

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

8 participants