Taro: HOOK useRef current的值拿不到,无法调用子组件的方法

Created on 16 Sep 2019  ·  27Comments  ·  Source: NervJS/taro

问题描述
按照官方文档使用useRef这个hook创建的节点,,在后面的点击事件中这个节点的current值为null

复现步骤
按照官网示例

`` import Taro, { useContext, useRef, Component } from "@tarojs/taro"; import { View, Button, Text,Input,Navigator } from "@tarojs/components"; function IndexPage() { const inputEl = useRef(null); const onButtonClick = () => { //current` 指向已挂载到 DOM 上的文本输入元素
console.log(inputEl);
// inputEl.current.focus();
};

return (


Most helpful comment

本周修一下

All 27 comments

欢迎提交 Issue~

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

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

Good luck and happy coding~

@Hsyneve 用法是 <Input ref={inputEl} type="text" />,不过还是会有问题,尽快修复。

谢谢大神知晓,我开始是按照这个写法来的,没有用我才改成了别的方式,报错都是一样

目前通过字符串的方式还是可以拿到。。虽然在 function component 中使用 this.refs.input 感觉很慌。。

有进展吗 @Chen-jj

这么基础的功能都出错了…

CC @yuche

通过字符串创建的可以用,useRef还是用不了,求解决

Taro CLI 1.3.20 environment info:
System:
OS: Windows 10
Binaries:
Node: 10.16.3 - ~\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
Yarn: 1.19.1 - ~\scoop\apps\nvm\current\nodejs\nodejs\yarn.CMD
npm: 6.9.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD

inputEl.current.focus(); current虽然可以取到值,但是无法调用dom的方法,有没有进展呢

@Chen-jj 大佬,能给个答复吗?

一个半月了哟。。

本周修一下

请问这个问题修复完发布了吗?刚刚更新到"1.3.22"问题依旧哦

什么时间修复呀,着急呀,开发的项目都快上线了

什么时间修复呀,着急呀,开发的项目都快上线了

老老实实用字符串ref吧orz

还没发版,尽快发一个

什么时间修复呀,着急呀,开发的项目都快上线了

老老实实用字符串ref吧orz

function component 怎么用字符串?

还没发版,尽快发一个

尽快呀老哥,就等你们临门一脚了

什么时间修复呀,着急呀,开发的项目都快上线了

老老实实用字符串ref吧orz

function component 怎么用字符串?

```

什么时间修复呀,着急呀,开发的项目都快上线了

老老实实用字符串ref吧orz

function component 怎么用字符串?

<Component ref="com" />
<script>
console.log(this.$refs.com);
</script>

function component里面this不是undefined吗???难道我任督二脉没打通?

function component里面this不是undefined吗???难道我任督二脉没打通?

没想到吧,还真的可以用,就是有点奇怪

@superman0159 1.3.23 发了,更新试一下?

@superman0159 1.3.23 发了,更新试一下?

哇我试试,爱你哟

@Chen-jj
更新1.3.23,安装依赖包报错
image

@Chen-jj
更新1.3.23,安装依赖包报错
image

同升级失败,折腾死我了

@gaogao2017 @pasBone 再试试把

image

好了

Was this page helpful?
0 / 5 - 0 ratings