【问题标题】:react native drawer navigation on Press Item action对 Press Item 操作做出反应本机抽屉导航
【发布时间】:2019-03-07 08:59:33
【问题描述】:

在我的抽屉导航器中,当我按下注销时,有一个注销按钮。我想删除app_token,但我不知道怎么做。

我尝试这样写:

onItemPress:() => { AsyncStorage.removeItem('app_token')},

但它不起作用。

const AppDrawerNavigator = createDrawerNavigator({



      Logout: {

    onItemPress:() => { AsyncStorage.removeItem('app_token')},

          screen: HomePage,
          navigationOptions: {


              drawerIcon: (
                  <Image style={{ width: 30, height: 30 }}
                         source={require('./assets/IconDrawerNavigation/logout.png')} />
              )
          }
      },

} );

【问题讨论】:

  • 你解决了吗?
  • 是的,我解决了,我会在下面添加评论

标签: react-native react-navigation asyncstorage react-navigation-drawer


【解决方案1】:

为了解决这个问题,我使用了这个道具中名为 contentComponent 的道具,你可以创建自己的抽屉

import drawerContentComponents from './Drawer';

const AppDrawerNavigator = createDrawerNavigator({

  Home: {screen: Home,}
  },

},
  {contentComponent: drawerContentComponents,}

  );

//抽屉源码

//i use this library so i can restart the app and logout 
import RNRestart from 'react-native-restart'; 

export default class drawerContentComponents extends Component {


    _logout = () => {
        AsyncStorage.removeItem('sale_id');
        RNRestart.Restart();

    }
    render() {


        return (

            <View style={styles.container2}>

                <TouchableOpacity onPress={() => this.props.navigation.navigate('profile')} >
                    <View style={styles.screenStyle}>
                        <Image style={styles.iconStyle}
                            source={home} />
                        <Text style={styles.screenTextStyle}>My Profile</Text>
                    </View>
                    <View style={styles.underlineStyle} />
                </TouchableOpacity>


                <TouchableOpacity onPress={() => this.props.navigation.navigate('social')} >
                    <View style={styles.screenStyle}>
                        <Image style={styles.iconStyle}
                            source={home} />
                        <Text style={styles.screenTextStyle}>Contact US</Text>
                    </View>
                    <View style={styles.underlineStyle} />
                </TouchableOpacity>



                <TouchableOpacity onPress={this._logout} >
                    <View style={styles.screenStyle}>
                        <Image style={styles.iconStyle}
                            source={home} />
                        <Text style={styles.screenTextStyle}>Logout</Text>
                    </View>

                </TouchableOpacity>


            </View>

        )
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多