0

Is there a way to call an event when I press on an item inside this ScrollView?

let notes = this.state.noteArray.map((val, key)=>{
    return <Note key={key} keyval={key} val={val}
        deleteMethod={()=>this.deleteNote(key)}
        editMethod={()=> this.editMethod(key, val)} />
});

<ScrollView style={styles.scrollContainer}> {notes} </ScrollView>

Note:

import React, { Component } from 'react';

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

export default class Note extends Component { render() { return ( {this.props.val.date} {this.props.val.note}

            <TouchableOpacity onPress={this.props.deleteMethod} style={styles.noteDelete}>
                <Text style={styles.noteDeleteText}>Del</Text>
            </TouchableOpacity>

            <TouchableOpacity onPress={this.props.editMethod} style={styles.editNote}>
                <Text style={styles.noteDeleteText}>Edit</Text>
            </TouchableOpacity>
        </View>
    );
}
}


const styles = StyleSheet.create({
note: {
    position: 'relative',
    padding: 20,
    paddingRight: 100,
    borderBottomWidth:2,
    borderBottomColor: '#ededed'
},
noteText: {
    paddingLeft: 20,
    borderLeftWidth: 10,
    borderLeftColor: '#0000FF'
},
noteDelete: {
    position: 'absolute',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#2980b9',
    padding: 10,
    top: 10,
    bottom: 10,
    right: 10
},
editNote: {
    position: 'absolute',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#2980b9',
    padding: 10,
    top: 10,
    bottom: 10,
    right: 70
},
noteDeleteText: {
    color: 'white'
},
});
3
  • are you passing a props to Note component ? Commented May 21, 2018 at 9:15
  • key and val. Let me place the Note component in the question Commented May 21, 2018 at 9:17
  • There you go... Commented May 21, 2018 at 9:19

2 Answers 2

0

You need to add your all items under TouchableOpacity in which you like to get an event:

 <TouchableOpacity               
      onPress={() => { 
                alert('Pressed')
      }}>
// Keep your element which you like to tap
</TouchableOpacity>

You can refer the react-native doc for more reference.

Sign up to request clarification or add additional context in comments.

2 Comments

If I do this, the event will apply if I press the ScrollView, not the item inside it.
wrap it inside a view. It'll work when clicked on anywhere inside the view. I don't prefer using buttons, so i use a combination of touchableHightlight with view and text.
0

You can use like this

let notes = this.state.noteArray.map((val, key)=>{
    return <Note key={key} keyval={key} val={val}
        deleteMethod={()=>this.deleteNote.bind(key)}
        editMethod={()=> this.editMethod.bind(key, val)} />
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.