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

Taro hook 组件中怎么获取当前组件的实列? #4521

Closed
hqwlkj opened this issue Sep 24, 2019 · 6 comments
Closed

Taro hook 组件中怎么获取当前组件的实列? #4521

hqwlkj opened this issue Sep 24, 2019 · 6 comments

Comments

@hqwlkj
Copy link
Contributor

hqwlkj commented Sep 24, 2019

问题描述
在使用 canvascreateCanvasContext 方法时,需要传入一个当前组件的实列;
使用hook,是获取不到当前组件的this,那么我们在使用createCanvasContext方法第二个参数传递什么呢?
例: const context: Taro.CanvasContext = Taro.createCanvasContext('poster-canvas', this);,
这里传入的this是获取不到的。

复现步骤

const Poster=()=>{
      useDidShow(() => drawBall());

      const drawBall=()=>{
            console.log('this', this); // => undefined
            const context: Taro.CanvasContext = Taro.createCanvasContext('poster-canvas', this);
      }

     return <View>
         <Canvas className='poster-canvas' canvasId='poster-canvas' />
     </View>
}
export default Poster;

期望行为
[这里请用简洁清晰的语言描述你期望的行为]

报错信息

[这里请贴上你的完整报错截图或文字]

系统信息
👽 Taro v1.3.18

Taro CLI 1.3.18 environment info:
System:
OS: macOS 10.14.6
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 12.9.1 - /usr/local/bin/node
Yarn: 1.10.1 - /usr/local/bin/yarn
npm: 6.11.3 - /usr/local/bin/npm
npmPackages:
@tarojs/components: 1.3.18 => 1.3.18
@tarojs/plugin-babel: 1.3.18 => 1.3.18
@tarojs/plugin-csso: 1.3.18 => 1.3.18
@tarojs/plugin-less: 1.3.18 => 1.3.18
@tarojs/plugin-uglifyjs: 1.3.18 => 1.3.18
@tarojs/rn-runner: 1.3.18 => 1.3.18
@tarojs/router: 1.3.18 => 1.3.18
@tarojs/taro: 1.3.18 => 1.3.18
@tarojs/taro-alipay: 1.3.18 => 1.3.18
@tarojs/taro-h5: 1.3.18 => 1.3.18
@tarojs/taro-swan: 1.3.18 => 1.3.18
@tarojs/taro-tt: 1.3.18 => 1.3.18
@tarojs/taro-weapp: 1.3.18 => 1.3.18
@tarojs/webpack-runner: 1.3.18 => 1.3.18
eslint-config-taro: 1.3.18 => 1.3.18
eslint-plugin-taro: 1.3.18 => 1.3.18
nerv-devtools: ^1.4.4 => 1.4.4
nervjs: ^1.4.4 => 1.4.4
stylelint-config-taro-rn: 1.3.18 => 1.3.18
stylelint-taro-rn: 1.3.18 => 1.3.18
npmGlobalPackages:
typescript: 3.5.1
补充信息

[可选]
[根据你的调查研究,出现这个问题的原因可能在哪里?]

@taro-bot
Copy link

taro-bot bot commented Sep 24, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@hqwlkj hqwlkj changed the title Taro hook 组件中怎么获取 当前组件的实列 Taro hook 组件中怎么获取当前组件的实列? Sep 24, 2019
@zzyyzz1992
Copy link

我感觉是不是加上一个hook就可以?
useCanvasContext

@hqwlkj
Copy link
Contributor Author

hqwlkj commented Sep 24, 2019

但是目前没有这个 useCanvasContext hook

@luckyadam
Copy link
Member

暂时写 this.$scope

@hqwlkj
Copy link
Contributor Author

hqwlkj commented Sep 24, 2019

this 都是一个 undefined ,怎么可能有 this.$scope

image

@fjc0k
Copy link
Contributor

fjc0k commented Oct 12, 2019

这里有个 Taro 工具库已实现 useScope: https://github.com/fjc0k/vtils/tree/master/packages/taro#usescope

@Chen-jj Chen-jj closed this as completed Jul 3, 2020
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

6 participants