Hopefully this is easy for someone...but I am getting stuck here. I am looking to map over a constant. One of the things I am wanting to map over is an onPress function...so in my constant I am including what I would like this function to be, but am having trouble with "this". The example should make the most sense...any help is appreciated on how to set this up correctly. I am using React Native, but I'm sure same principle applies to React.
const ITEMS = [
{ title: 'Item 1', subTitle: 'Click here for Item 1 function', onPress: () => this.function1() },
{ title: 'Item 2', subTitle: 'Click here for Item 2 function', onPress: () => this.function2() }
]
class Page extends Component {
function1 = () => {
console.log('This is function 1');
}
function2 = () => {
console.log('This is function 2');
}
render() {
return (
<View>
{_.map(ITEMS, (item, itemIndex) => {
return (
<Item
key={itemIndex}
title={item.title}
subTitle={item.subTitle}
onPress={item.onPress}
index={itemIndex}
/>
);
})}
</View>
);
}
export default Page;
The error I continue to receive is:
_this.function1 is not a function
Does anybody know how I can set this up properly? Thanks!