taro refs引用

创建 Refs

  • Taro 支持使用字符串和函数两种方式创建 Ref:
  1. 使用字符串创建 ref
  2. 通过函数创建 ref(推荐)
    你也可以通过传递一个函数创建 ref, 在函数中被引用的组件会作为函数的第一个参数传递。如果是被引用的组件是自定义组件,那可以在任意的生命周期访问引用。
    不管在任何情况下,Taro 都推荐你使用函数的方式创建 ref。
class MyComponent extends Component {

  roar () {
    // 会打印 `miao, miao, miao~`
    this.cat.miao()
  }

  refCat = (node) => this.cat = node // `this.cat` 会变成 `Cat` 组件实例的引用

  render () {
    return <Cat ref={this.refCat} />
  }
}

class Cat extends Components {
  miao () {
    console.log('miao, miao, miao~')
  }

  render () {
    return <View />
  }
}

猜你喜欢

转载自www.cnblogs.com/cag2050/p/9930015.html
今日推荐