inhoLee
inhoLee

Reputation: 219

React Native get Clicked(pressed) component's attribute

I'm making some dynamic CardView which can show videos like as Facebook.

But there's problem when I press share button and comment button.

Because I need the clicked button's(share, comment) index.

So I think I can use the key prop of button.

I make buttons with touchableOpacity, then I'll set key of touchableOpacity like this.

<TouchableOpacity
  key = {some index}
>
  some stuffs
</TouchableOpacity>

Then, I want to get pressed component's attr value( key value at here )

I can find the key value at

ReactNativeComponentTree.getInstanceFromNode(event.nativeEvent)

But this data always show my key is null.

There's no way to get clicked Component's key value?

Thanks for reading.

Upvotes: 2

Views: 1089

Answers (1)

Tholle
Tholle

Reputation: 112927

If you really need the index in the onPress handler, you can create a new inline function and pass along the index instead.

Example

<TouchableOpacity
  key={someIndex}
  onPress={() => this.handlePress(someIndex)}
>
  some stuff
</TouchableOpacity>

Upvotes: 4

Related Questions