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 微信小程序 useImperativeHandle 问题 #6069

Closed
WuYunlong opened this issue Apr 22, 2020 · 6 comments · Fixed by #6374
Closed

taro 微信小程序 useImperativeHandle 问题 #6069

WuYunlong opened this issue Apr 22, 2020 · 6 comments · Fixed by #6374
Assignees

Comments

@WuYunlong
Copy link

问题描述

组件代码

import Taro,{useImperativeHandle} from '@tarojs/taro';
import {View} from '@tarojs/components';

const Demo = ({childRef}) => {
  useImperativeHandle(childRef, () => ({
    stop () {
      console.log("======")
    }
  }))
  return (
      <View className="demo">111</View>
  )
}

export default Demo;

页面代码

import Taro,{useRef} from '@tarojs/taro';
import {View, Button} from '@tarojs/components';
import Demo from '../components/Demo';

const User = () => {
  let childRef = useRef();

  const handelClick = () => {
    console.log(childRef.current);
    childRef.current.stop()
  }

  return(
      <View className="wrap wrap-user">
        <Button onClick={handelClick}>测试</Button>
        <Demo childRef={childRef} />
      </View>
  )
}

export default User;

页面点击测试按钮控制台报错

image


### 期望行为

能够正常执行到子组件到stop方法。

### 系统信息

```bash
👽 Taro v2.1.5


  Taro CLI 2.1.5 environment info:
    System:
      OS: macOS 10.15.4
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 13.12.0 - ~/.nvm/versions/node/v13.12.0/bin/node
      Yarn: 1.22.4 - ~/.nvm/versions/node/v13.12.0/bin/yarn
      npm: 6.14.4 - ~/.nvm/versions/node/v13.12.0/bin/npm
    npmPackages:
      @tarojs/components: 2.1.5 => 2.1.5 
      @tarojs/components-qa: 2.1.5 => 2.1.5 
      @tarojs/mini-runner: 2.1.5 => 2.1.5 
      @tarojs/mobx: 2.1.5 => 2.1.5 
      @tarojs/mobx-h5: 2.1.5 => 2.1.5 
      @tarojs/router: 2.1.5 => 2.1.5 
      @tarojs/taro: 2.1.5 => 2.1.5 
      @tarojs/taro-alipay: 2.1.5 => 2.1.5 
      @tarojs/taro-h5: 2.1.5 => 2.1.5 
      @tarojs/taro-qq: 2.1.5 => 2.1.5 
      @tarojs/taro-quickapp: 2.1.5 => 2.1.5 
      @tarojs/taro-swan: 2.1.5 => 2.1.5 
      @tarojs/taro-tt: 2.1.5 => 2.1.5 
      @tarojs/taro-weapp: 2.1.5 => 2.1.5 
      @tarojs/webpack-runner: 2.1.5 => 2.1.5 
      eslint-config-taro: 2.1.5 => 2.1.5 
      eslint-plugin-taro: 2.1.5 => 2.1.5 
      nerv-devtools: ^1.5.6 => 1.5.6 
      nervjs: ^1.5.6 => 1.5.6 
      stylelint-config-taro-rn: 2.1.5 => 2.1.5 
      stylelint-taro-rn: 2.1.5 => 2.1.5 
@taro-bot
Copy link

taro-bot bot commented Apr 22, 2020

CC @Chen-jj

@taro-bot
Copy link

taro-bot bot commented Apr 22, 2020

欢迎提交 Issue~

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

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

Good luck and happy coding~

@WuYunlong
Copy link
Author

控制台报错信息如下

Cannot read property 'stop' of undefined; [Component] Event Handler Error @ pages/user#(anonymous)
TypeError: Cannot read property 'stop' of undefined
    at User.handelClick [as anonymousFunc0] (http://127.0.0.1:21945/appservice/pages/user.js:79:26)
    at Se.obj.<computed> [as anonymousFunc0] (http://127.0.0.1:21945/appservice/taro.js:2454:36)
    at Object.r.safeCallback (http://127.0.0.1:21945/appservice/__dev__/WAService.js:2:1467232)
    at http://127.0.0.1:21945/appservice/__dev__/WAService.js:2:1586042
    at c (http://127.0.0.1:21945/appservice/__dev__/WAService.js:2:1593979)
    at http://127.0.0.1:21945/appservice/__dev__/WAService.js:2:1585967
    at r (http://127.0.0.1:21945/appservice/__dev__/WAService.js:2:1538494)
    at http://127.0.0.1:21945/appservice/__dev__/WAService.js:2:1538616
    at http://127.0.0.1:21945/appservice/__dev__/WAService.js:2:726082
    at n (http://127.0.0.1:21945/appservice/__dev__/asdebug.js:1:27118)

@WuYunlong
Copy link
Author

@luckyadam 这个问题一直没有解决掉 能否看一下

@WuYunlong
Copy link
Author

@luckyadam 这个问题一直没有解决掉 能否看一下

@Sanford284
Copy link

3.0.8 有这个问题

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

Successfully merging a pull request may close this issue.

3 participants