I have a problem with if/else statements in React Native.
For example, I have two states:
onOpen() {
this.setState({
index: 1
});
}
onClose() {
this.setState({
index: 0
});
}
I need to set State index: 0, when toggle close and index: 1, when toggle open.
<TouchableOpacity onPress={() =>
{index === 0 ? navigator.toggleDrawer(this.onClose())
: navigator.toggleDrawer(this.onOpen())}}>
For now, when I click on menu-button I receive index 0 every time, but they should change on 1 and back. Thanks.
Component:
import React from 'react';
import {
TouchableOpacity,
Image
} from 'react-native';
export default class Home extends Component {
constructor(props) {
super(props);
this.state = {
index: 0
};
}
onOpen() {
this.setState({
index: 1
});
}
onClose() {
this.setState({
index: 0
});
}
render() {
const { index } = this.state;
return (
<TouchableOpacity onPress={() => {index === 1 ?
navigator.toggleDrawer(this.onClose()) :
navigator.toggleDrawer(this.onOpen())}}>
<Image
source={require('something.png')}
/>
</TouchableOpacity>
);
}
}
Aucun commentaire:
Enregistrer un commentaire