My problem is that I cannot access a function of the same class from a button that I have in the header.
I would like to know how to access
I have made two functions, one similar to the change of screen from the header, but trying to access a function instead of another screen.
And another way suggested by someone else which doesn't work either.
class HomeScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
return {
headerTitle: ('',
...
),
headerLeft: (
...
),
headerRight: (
<TouchableHighlight onPress={() => { this._function() }}>
<Image style={{ width: 30, height: 30, marginLeft: 10 }}
source={require('./images/reportIcon.png')}
/>
</TouchableHighlight>
),
//Segunda manera
headerRight: (
<TouchableHighlight onPress: () => { navigation.getParam('report') }>
<Image style={{ width: 30, height: 30, marginLeft: 10 }}
source={require('./images/reportIcon.png')}
/>
</TouchableHighlight>
)
}
}
constructor(props) {
super(props);
this.state = {
...
}
}
_function = () => {
Alert.alert(
'Titutlo',
'Blablabla',
[{ text: 'Entendido' }]
);
}
}
The first way sends me an error.
The second one does not mark errors but does not give any action, BEWARE: I have the function in ComponentDidMount, but it still doesn't work
It is normal that you do not see that any function does something, the first because it is a static method, that is to say that it does not have an instance but rather that it belongs to the class, that is, it is incorrect to call
this
when you define a method withstatic
, the ideal would be SoHomeScreen._function()
, which may not work well for you, since_function
if it is an instance method, the ideal would be to do something like this:Or else convert
_function
to static, like so:But remember that inside a function that you define with
static
you should not usethis
.The second option doesn't do anything because you're not telling it to do anything, you're just telling it to execute the method
navigation.getParam('report')
but you're neither returning nor treating the data in any way, you're not changing anything, nor calling any other internal function, you're not logging either, so you don't notice anything happening.Any concern, ask