公司领导不展示

时间:2019-11-20 06:34:02

标签: javascript react-native

因此,我正在尝试完成来自Coursera的使用React Native进行多平台移动应用开发的第一周的作业。

在一项任务中,我应该在“关于我们”导航项中显示公司负责人。

export const LEADERS = [
  {
    id: 0,
    name: 'Peter Pan',
    image: '/assets/images/alberto.png',
    designation: 'Chief Epicurious Officer',
    abbr: 'CEO',
    featured: false,
    description: "Our CEO, Peter, credits his hardworking East Asian immigrant parents who undertook the arduous journey to the shores of America with the intention of giving their children the best future. His mother's wizardy in the kitchen whipping up the tastiest dishes with whatever is available inexpensively at the supermarket, was his first inspiration to create the fusion cuisines for which The Frying Pan became well known. He brings his zeal for fusion cuisines to this restaurant, pioneering cross-cultural culinary connections."
  },
  {
    id: 1,
    name: 'Dhanasekaran Witherspoon',
    image: '/assets/images/alberto.png',
    designation: 'Chief Food Officer',
    abbr: 'CFO',
      featured: false,
    description: 'Our CFO, Danny, as he is affectionately referred to by his colleagues, comes from a long established family tradition in farming and produce. His experiences growing up on a farm in the Australian outback gave him great appreciation for varieties of food sources. As he puts it in his own words, Everything that runs, wins, and everything that stays, pays!'
  },
  {
    id: 2,
    name: 'Agumbe Tang',
    image: '/assets/images/alberto.png',
    designation: 'Chief Taste Officer',
    abbr: 'CTO',
      featured: false,
    description: 'Blessed with the most discerning gustatory sense, Agumbe, our CFO, personally ensures that every dish that we serve meets his exacting tastes. Our chefs dread the tongue lashing that ensues if their dish does not meet his exacting standards. He lives by his motto, You click only if you survive my lick.'
  },
  {
    id: 3,
    name: 'Alberto Somayya',
    image: '/assets/images/alberto.png',
    designation: 'Executive Chef',
    abbr: 'EC',
    featured: true,
    description: 'Award winning three-star Michelin chef with wide International experience having worked closely with whos-who in the culinary world, he specializes in creating mouthwatering Indo-Italian fusion experiences. He says, Put together the cuisines from the two craziest cultures, and you get a winning hit! Amma Mia!'
  }
];

在MainComponent中,我定义了所有导航器

import React, { Component } from 'react';
import Menu from './MenuComponent';
import Dishdetail from './DishDetailComponent';
import { View, Platform } from 'react-native';
import { createStackNavigator, createDrawerNavigator } from 'react-navigation';
import Home from './HomeComponent';
import About from './AboutComponent';
import Contact from './ContactComponent';

const MenuNavigator = createStackNavigator({
    Menu: { screen: Menu },
    Dishdetail: { screen: Dishdetail }
}, {
    initialRouteName: 'Menu',
    navigationOptions: {
      headerStyle: {
        backgroundColor: '#512DA8'
      }, 
      headerTintColor: '#fff',
      headerTitleStyle: {
        color: '#fff'
      }
    }
});

const HomeNavigator = createStackNavigator({
  Home: { screen: Home }
}, {
    navigationOptions: {
        headerStyle: {
            backgroundColor: '#512DA8'
        },
        headerTintColor: '#fff',
        headerTitleStyle: {
            color: '#fff'
        }
    }
});

const AboutNavigator = createStackNavigator({
  About: { screen: About },
}, {
  navigationOptions: {
      headerStyle: {
          backgroundColor: '#512DA8'
      },
      headerTintColor: '#fff',
      headerTitleStyle: {
          color: '#fff'
      }
  }
});

const ContactNavigator = createStackNavigator({
  Contact: { screen: Contact },
}, {
  navigationOptions: {
      headerStyle: {
          backgroundColor: '#512DA8'
      },
      headerTintColor: '#fff',
      headerTitleStyle: {
          color: '#fff'
      }
  }
});

const MainNavigator = createDrawerNavigator({
  Home: {
      screen: HomeNavigator,
      navigationOptions: {
          title: 'Home',
          drawerLabel: 'Home'
      }
  },
  About: {
    screen: AboutNavigator,
    navigationOptions: {
        title: 'About Us',
        drawerLabel: 'About Us'
    }    
  },
  Menu: {
      screen: MenuNavigator,
      navigationOptions: {
          title: 'Menu',
          drawerLabel: 'Menu'
      }    
  },
  Contact: {
    screen: ContactNavigator,
    navigationOptions: {
        title: 'Contact Us',
        drawerLabel: 'Contact Us'
    }    
  },
}, {
  drawerBackgroundColor: '#D1C4E9',
})

class Main extends Component {

  render() {
    return (
      <View style={{flex:1}}>
        <MainNavigator />
      </View>
    );
  }
}

export default Main;

最后,我定义了AboutComponent.js,试图在其中显示公司领导者列表。

import React, { Component } from 'react';
import { FlatList,ScrollView, Text,View } from 'react-native';
import LEADERS from '../shared/leaders';
import { ListItem, Card } from 'react-native-elements';

class About extends Component {

    constructor(props){
        super(props);
        this.state = {
            leaders: LEADERS
        }
    }

    static navigationOptions = { 
        title: 'About us'
    }


    render(){

        const renderLeaderItem = ({item, index}) => {

            return (
                    <ListItem
                        key={index}
                        title={item.name}
                        subtitle={item.description}
                        hideChevron={true}
                        leftAvatar={{ source: require('./images/uthappizza.png') }}
                      />
            );
        };

        return (
            <ScrollView>
                <Card title="Our History">
                    <Text>Started in 2010, Ristorante con Fusion quickly established itself as a culinary icon par excellence in Hong Kong. With its unique brand of world fusion cuisine that can be found nowhere else, it enjoys patronage from the A-list clientele in Hong Kong.  Featuring four of the best three-star Michelin chefs in the world, you never know what will arrive on your plate the next time you visit us.</Text>
                    <Text>The restaurant traces its humble beginnings to The Frying Pan, a successful chain started by our CEO, Mr. Peter Pan, that featured for the first time the world's best cuisines in a pan.</Text>
                </Card>
                <Card title="Corporate Leadership">                    
                    <FlatList 
                        data={this.state.leaders}
                        renderItem={renderLeaderItem}
                        keyExtractor={item => item.id.toString()}
                    />
                </Card>
            </ScrollView>
        )
    }

}


export default About;

为什么会这样?这是我的repo

1 个答案:

答案 0 :(得分:1)

不允许像在renderLeaderItem中那样在渲染函数中定义const函数。

如何修改代码:

在外部渲染函数中创建renderLeaderItem,例如:

   renderLeaderItem(item, index) {
            return (
                    <ListItem
                       ...
                      />
            );
    }
    render(){ 
       return ( 
       ... 
       );
    } 

将FlatList中的代码更改为:

<FlatList 
 data={this.state.leaders}
 renderItem={({item, index}) => this.renderLeaderItem(item,index)}
 keyExtractor={item => item.id.toString()}
/>

工作演示:

https://snack.expo.io/BkFRAwznH