Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set an item onTap callback? #33

Closed
gds3k3 opened this issue Mar 24, 2018 · 4 comments
Closed

How to set an item onTap callback? #33

gds3k3 opened this issue Mar 24, 2018 · 4 comments

Comments

@gds3k3
Copy link

gds3k3 commented Mar 24, 2018

Hello, many thanks for you work!

I have two questions:

  1. how to set a item onTap callback?

  2. Any chance to have add/del item functionality?

Thanks again.

@saleel
Copy link
Owner

saleel commented Mar 24, 2018

Hi there,

You are in full control of the item you want to present in the grid. You can define your own TouchableOpacity in renderItem and have your own onTap equivalent callbacks.

Similarly, delete functionality can be implemented on your end by adding a delete button and on clicking that you can remove the item from the items array. If this items is in your state and the same state variable is passed the grid as items prop, then the grid will re-render.

@saleel saleel closed this as completed Mar 24, 2018
@gds3k3
Copy link
Author

gds3k3 commented Mar 24, 2018

Thanks for you fast response.

Already tried a TouchableOpacity around each item, it get triggered on initial render and never more.

				renderItem={item => (
					<TouchableOpacity 
						onPress={this.props.click(item.name)} 
						underlayColor="blue">
						<View style={[styles.itemContainer, { backgroundColor: item.code }]} >
							<Image
									style={styles.button}
									source={require('../assets/images/robot-dev.png')}
								  />						
							<Text style={styles.itemName}>{item.name}</Text>
							<Text style={styles.itemCode}>{item.code}</Text>
						</View>
					</TouchableOpacity>				
				)}

Sorry I am just starting with react/native, it is probably me doing something wrong.

Thanks you.

@saleel
Copy link
Owner

saleel commented Mar 24, 2018

Try onPress={() => this.props.click(item.name)}

onPress expects a function as the value. You are calling the function there, and onPress gets the return value from props.click and not the actual function.

props.click is executed for each item when the component is rendered initially, which is why you saw the click results on first load (even without clicking the item I suppose).

@gds3k3
Copy link
Author

gds3k3 commented Mar 24, 2018

Thats works. It can bubble the click correctly now.
Many thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants