【问题标题】:accessing props using ref call back使用 ref 回调访问道具
【发布时间】:2019-05-10 18:40:43
【问题描述】:

我正在努力挠头;试图找出下面的 sn-p 出了什么问题。

import React from 'react';
import { Text, TouchableOpacity, View } from 'react-native';

class MyButton extends React.Component {

  setNativeProps = (nativeProps) => {
    alert(JSON.stringify(this._root.props)) //able to get this.v here
  }

  render() {
    return (
      <View ref={cc => {this._root = cc; this.v = 100 }} me="tom">
        <Text ref={component => { this._root1 = component;}} style={{margin:55}} onPress={()=>this.setNativeProps({text:'fgfg'})}>{this.props.label} </Text>
      </View>
    )
  }
}

export default class App extends React.Component {
  render() {
    return (
      <TouchableOpacity >
        <MyButton label="Press me!" />
      </TouchableOpacity>
    )
  }
}

基本上尝试使用 ref 回调

&lt;View&gt; 元素(即this._root.props)获取道具

尽管this._root1.props 一直都可以正常工作。 有人可以帮我弄清楚它有什么问题吗?

编辑: 我什至可以看到this._root,但我什至看不到 this._root.props.me。

【问题讨论】:

    标签: react-native view prop


    【解决方案1】:

    你能不能尽量不做

    警报(JSON.stringify(this._root.props))

    而只是做

    警报(this._root.props)

    即删除 JSON.stringify

    它不起作用的原因是因为 View 本身有一个子元素,而使用 Text 它没有任何子元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 2023-01-29
      • 2018-10-09
      相关资源
      最近更新 更多