如何使用 onPress 定位节点而不是节点号 [英] How to target the node instead of the node number with an onPress

查看:45
本文介绍了如何使用 onPress 定位节点而不是节点号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一群用户被渲染到 中的 组件中.

I have a bunch of users who are being rendered into <StatusSphere/> components in the <ListView/>.

当我点击每个 <StatusSpheres/> 我希望它们各自的 uid 打印到控制台(以便我可以进一步操作它们).

When I click on each of the <StatusSpheres/> I would like their individual uid's to print to the console (so I can further manipulate them).

这就是我遇到麻烦的地方;在 React 上,我会使用 e.target.value,甚至可以使用 getAttributes.但在这里我得到的只是一个数字(例如 237 或 248 .. 等),我认为它是节点号?

This is where I'm having trouble; on React I would use e.target.value, or even could use getAttributes. But here all I am getting back is a number (for example 237 or 248.. etc) which I believe is the node number?

我发现另一个消息来源说你可以使用:

I have found another source that says you can use:

var ReactNativeComponentTree =require('react/lib/ReactNativeComponentTree');ReactNativeComponentTree.getInstanceFromNode(nativeTag);

但这对我不起作用,而且看起来很奇怪.

but that didn't work for me and seems weird anyway.

如何轻松访问已存储在实际组件上的 uid?肯定有我没遇到过的直截了当的方法.

How can I easily access the uid which is already stored on the actual component? There must be a straightforward way that I haven't come across.

非常感谢

export default class UserList extends Component {
  constructor() {
    super();
    this.userRef = firebase.database().ref().child('users')
    this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
    this.state = {
      users: this.ds.cloneWithRows([])
    }
  }
  componentWillMount() {
    //Checks my firebase database for the users and pushes it into the this._users array....
    this._users = []
    this.userRef.on('child_added', snap => {
      this._users.push({
        user: snap.val().username,
        isOnline: snap.val().isOnline,
        isChatter: snap.val().isChatter,
        uid: snap.val().uid
      });
      //...which then gets passed into this function...
      this.populateUsers(this._users);
    }).bind(this);
  }
  populateUsers(nodes) {
    //which then populates the state with the databases' users, which is then automatically rendered in the listview below
    this.setState({users: this.ds.cloneWithRows(nodes)});
  }
  _toChat(e) {
    console.log('************ native target *************');
    console.log(e.target);
    console.log('************ native target *************');
  }
  render() {
    return (
      <ListView
        horizontal
        enableEmptySections={true}
        dataSource={this.state.users}
        renderRow={d => <StatusSphere onPress={this._toChat}
                                      uid={d.uid}
                                      user={d.user}
                                      isOnline={d.isOnline}
                                      isChatter={d.isChatter}/>}/>
    )
  }
}

推荐答案

我不确定我是否理解你的问题,但是 ;

I'm not sure I have understand your question but ;

您可以使用箭头函数调用带有额外参数的 toChat 方法.

you can call toChat method with extra parameter using arrow function.

onPress={(e)=>{this._toChat(e,d.uid)}}

这篇关于如何使用 onPress 定位节点而不是节点号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆