question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Button.onPress parameter not documented

See original GitHub issue

Description

The onPress documentation for Button does not explain what argument(s) may be passed.

I visited https://facebook.github.io/react-native/docs/button.html . I expected to find documentation describing the argument passed to onPress. The documentation seems to suggest that no arguments are passed () => any.

Reproduction Steps

Do the stuff in the description.

Sample Code

N/A

Solution

The documentation would be updated to show the parameter that is passed to the onPress callback. Ideally, it’d link to the documentation of that parameter.

Additional Information

  • React Native version: v0.46.0-rc.2 (current master)
  • Platform: N/A
  • Development Operating System: N/A
  • Build tools: N/A

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
McPocommented, Aug 19, 2017

So should the Event not then be documented. I believe that is what @dpkirchner is getting at. As It did not appear to me that onPress was called with any parameters, where as it is apparently being called with an Event object. The next question is what is within the Event object.

Im interested in this, to see if theres a way for me to get an attribute from the button element that was pressed.

Or am I missing something?


For those who are curious, I was able to obtain the props of the button that was pressed via

export const getPropsFromEvent = event => (event._targetInst._currentElement.props.children[0] || event._targetInst._currentElement.props.children)._owner._currentElement.props;

This does not appear to be officially supported, so beware that it could change at anytime. To print out the structure of the event object you can use the following.

onPress={ e => {
    seen = [];
  
  console.error(JSON.stringify(e,(key, val) => {
		   if (val != null && typeof val == "object") {
				if (seen.indexOf(val) >= 0) {
					return;
				}
				seen.push(val);
			}
			return val;
		}));
	}

What is the likelihood that the ability to access the props of the item that was pressed would be accepted as an official feature in react-native. In ReactJS, I tend to use the data attributes in order to pass parameters to onClick functions without having to bind them. Something like:

<Button data-id={ item.id } onClick={ props.onClick }>
...
props.onClick = event = > { console.log(parseInt(event.target.dataset.id))); }

Obviously I cant use data attributes in react-native, but something of a similar ilk would be nice. Should I create a separate issue for this?

1reaction
hramoscommented, Jun 20, 2017

The docs are generated from the source code. This is effectively calling http://facebook.github.io/react-native/docs/touchablewithoutfeedback.html#onpress, and from what I can see the only parameter passed is an Event https://github.com/facebook/react-native/blob/master/Libraries/Components/Touchable/TouchableWithoutFeedback.js#L119

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass a string parameter in an onclick function - Stack Overflow
When I click on this button, I have an error that says that "Add is not defined". Since this function call works perfectly...
Read more >
How Insidious React Native onPress Could Be - Bam Tech
The problem comes from the "secret" onPress parameter. Here is the ReactNative onPress type: ((event: GestureResponderEvent) => void) | ...
Read more >
Button | Android Developers
If you are not satisfied with the default button style, you can customize it. For more details and code samples, see the Styling...
Read more >
ASP.NET Core Razor components - Microsoft Learn
Learn how to create and use Razor components in Blazor apps, including guidance on Razor syntax, component naming, namespaces, and component ...
Read more >
Actions - Storybook - JS.ORG
Thousands of teams use it for UI development, testing, and documentation. ... title: 'Button', component: Button, argTypes: { onClick: { action: 'clicked' } ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found