javascript - ListItem onPress 无法正常工作

标签 javascript react-native jsx listitem expo

我正在使用React Native和react-native-elements在侧边菜单中呈现选项列表,但是当我运行该项目时,ListItem中的onPress Prop code> 未能发挥应有的作用。运行时,三个控制台日志似乎会自动迭代屏幕上的主要内容,但是当打开侧面菜单时,无法按下列表项,并且控制台中不会再记录任何内容。我的 onPress 函数有问题吗?

import { SideMenu, List, ListItem } from 'react-native-elements';
import React, { Component } from 'react';
import { View, StyleSheet } from 'react-native';
import { MaterialIcons } from '@expo/vector-icons';

export default class Home extends Component {

state = {
    toggled: false,
  }

  toggleSideMenu () {
  this.setState({
    toggled: !this.state.toggled
  })L

render() {
  const list = [
  {
    name: 'Profile',
    pic: 'assignment-ind',
    //nav: this.props.navigation.navigate('Profile', { user: this.state.user })
    nav: console.log('pressing Profile!'),
  },
  {
    name: 'Settings',
    pic: 'settings-applications',
    nav: console.log('pressing Settings!'), 
  },
  {
    name: 'Logout',
    //nav: firebase.auth().signOut() // sign user out 
    nav: console.log('pressing Logout!'),
  }

]
  const MenuComponent = (
    <View style={{flex: 1, backgroundColor: 'white', paddingTop: 10}}>
      <List containerStyle={{marginBottom: 20}}>
      {
        list.map((item, i) => (
          <ListItem
            onPress={() => item.nav} 
            leftIcon={{ name: item.pic }}
            key={i}
            title={item.name} 

          />
        ))
      }
      </List>
    </View>
  );

    return (
      <SideMenu
        menu={MenuComponent}
        toggled={this.state.toggled}>
        <View style={styles.container}>
        <View style={{ flexDirection: 'row' }}>
          <TouchableHighlight
            style={{marginTop: 20, marginLeft: 9}}
            onPress={this.toggleSideMenu.bind(this)}
            underlayColor='transparent'>
            <MaterialIcons
              color='white'
              name='menu'
              size={28}
            />
          </TouchableHighlight>
        </View>

        </View>
      </SideMenu>
    );
  }
}

  const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'black',
  },
});

最佳答案

弄清楚了:

我在列表对象中创建了每个正确的方法:

const list = [
  {
    name: 'Profile',
    pic: 'assignment-ind',
    //nav: this.props.navigation.navigate('Profile', { user: this.state.user })
    nav: () => { console.log('pressing Profile!'); },
  },
  {
    name: 'Settings',
    pic: 'settings-applications',
    nav: () => { console.log('pressing Settings!'); }, 
  },
  {
    name: 'Logout',
    //nav: firebase.auth().signOut() // sign user out 
    nav: () => { console.log('pressing Logout!'); },
  }

然后我在 onPress 函数中添加了括号:

onPress={() => item.nav()} 

关于javascript - ListItem onPress 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690032/

相关文章:

javascript - 真的通过 C#.NET 2.0 或更高版本检测浏览器支持 Javascript 吗?

javascript - Emscripten 中的 "Big"整数

javascript - Firebase RememberMe 功能

javascript - Android 上如何防止屏幕锁定时模态窗口消失?

javascript - 从映射对象中删除空数组

reactjs - React JSX错误: Unclosed regular expression

javascript - 在移动设备上切换 bootstrap input-group-addon 和 input 的位置

react-native - react native : FlatList is not showing

react-native - 使用 apollo 客户端 react native android 上传图像时获取网络请求失败

javascript - 类型错误 : Failed to execute 'removeChild' on 'Node' : parameter 1 is not of type 'Node' in React