In one of my classes, there's a method that I need to call. Specifically, the
() => this.props.navigation.navigate('Detail')
. Originally, I had a button on each FeedCard for navigation, but it seemed messy so I switched to using touchable opacity instead.
HomeScreen.js
class HomeScreen extends Component {
state = { }
render() {
return(
<Root>
<List>
<FeedCard />
<FeedCard doThis={() => this.props.navigation.navigate('Detail')} />
<FeedCard />
<FeedCard />
<FeedCard />
<FeedCard />
<FeedCard />
</List>
</Root>
)
}
};
Can functions be passed in React Native just like how I did with doThis()
?
FeedCard.js
function FeedCard({doThis}) {
return (
<Root>
<TouchableOpacity onPress={doThis}>
<FeedCardHeader />
<CardContainer>
<CardContentText>
{text}
</CardContentText>
</CardContainer>
<FeedCardBottom />
</TouchableOpacity>
</Root>
)
};
When I directly added the function to the onPress()
in the <TouchableOpacity>
component, it gave me the error
TypeError: undefined is not an object (evaluating '_this.props')
, which made me wonder if passing a function similar to my attempt above was even possible. Unfortunately, the code above doesn't navigate as intended. I am aware that navigation works because when I placed it in an onPress()
within a <Button>
on the same page as the HomeScreen, it worked perfectly.