javascript - If/else statements in React Native -
i have problem if/else statements in react native.
for example, have 2 states:
onopen() { this.setstate({ index: 1 }); } onclose() { this.setstate({ index: 0 }); }
i need set state index: 0, when toggle close , index: 1, when toggle open.
<touchableopacity onpress={() => {index === 0 ? navigator.toggledrawer(this.onclose()) : navigator.toggledrawer(this.onopen()) } }>
for now, when click on menu-button receive index 0 every time, should change on 1 , back. thanks.
component:
import react 'react'; import { touchableopacity, image } '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> ); } }
export default class home extends component { constructor(props) { super(props); this.state = { index: 0 }; this.toggleindex = this.toggleindex.bind(this); } toggleindex() { navigator.toggledrawer(); this.setstate({ index: this.state.index === 0 ? 1 : 0 }); } render() { return ( <touchableopacity onpress={toggleindex}> <image source={require('something.png')} /> </touchableopacity> ); } }
Comments
Post a Comment