TypeError:无法读取React Native中未定义的属性'map

时间:2019-02-04 10:56:00

标签: reactjs react-native state tabbar react-props

我正在做本机项目。我有数据数组,并且在render方法中,我试图循环它,就像一些自定义选项卡一样。但是,加载后,我试图从一个选项卡切换到另一个选项卡,它的抛出错误和崩溃像 enter image description here

TypeError: Can't read property 'map of undefined in React Native .

我的代码是

dashboard.js
constructor(props) {
    super(props);
    this.state = { 
    selectedIndex:0,
    tabList:[
      {tabName: ‘Telugu’, tabActiveImage:TeluguActiveImg, tabInactiveImage: TeluguInActiveImg, tabActiveText:'black', tabInactiveText: 'gray'},
      {tabName: ‘Tamil’, tabActiveImage:TeluguActiveImg, tabInactiveImage: TeluguActiveImg, tabActiveText:'black', tabInactiveText: 'gray'},
      {tabName: ’Hindi’, tabActiveImage: HindiActiveImg, tabInactiveImage: HindiInActiveImg, tabActiveText:'black', tabInactiveText: 'gray'},
      {tabName: ‘English’, tabActiveImage: EnglishActiveImg, tabInactiveImage: EnglishInActiveImg, tabActiveText:'black', tabInactiveText: 'gray'},
    ]
}
 }

OnTabItemHandler = (index) => {
    this.setState({selectedIndex:index})
    console.log('selected index is',index)

}

    render(item) {
        const {tabList} = this.state;
        return (
<View>Some static data loading </View>
 <View style = {styles.tabContainer}>
            {
            //loop throught the state
            this.state.tabList.map((item,index)=>{
              return(
                //the style just to make it beautiful and easy to debug
                <View style ={styles.tabInnerContainer}>
                <TouchableOpacity style={styles.tabIcons}
                  //this onpress to handle of active selected tab
                onPress={()=>this.OnTabItemHandler(index)}
                >
                    <Image
                      //here's the magic show off
                      source={this.state.selectedIndex=index?item.tabActiveImage:item.tabInactiveImage}
                      style={styles.tabItemsImages}
                    />
                    <Text style={styles.tabItemTextBlackColor}>{item.tabName}</Text>
                </TouchableOpacity>
                </View>
              )
            })
        }
              </View>
              {this.renderBottomContent(item)}
          </View>


      );
    }
  }
  

,底视图是   基于标签,我正在更改底视图

 renderBottomContent = (item) => {


    this.state = { dataArray: getListData()}    

    switch(selectedTab) {
      case "Telugu":
        return <View style = {styles.flatListContainer}>  
    //show flat list data
         }
          ItemSeparatorComponent = {() => (
              <View style={{height:15, backgroundColor:'blue'}/>
          )}
      />
    </View >
      case "Tamil":
        return <View style = {styles.bottomStaicScreensForTabs}>
            <Text>
                Tamil feature will come
                </Text>
                </View>
     case "Hindi":
        return <View style = {styles.bottomStaicScreensForTabs}>
            <Text>
                Hindi feature will come
                </Text>
                </View>
    default:
        return <View><Text></Text></View>
    }
  }
  

而且制表符文本的颜色不变,总是黑色。能够   任何人都可以帮助我,我做错了。

enter image description here

1 个答案:

答案 0 :(得分:1)

为了更好地理解问题,我创建了一个snack。我将在此处发布代码,以防其不再可用。 注意:它的样式不正确,图像也不符合您的预期,但是可以很好地重现该问题。

  constructor(props) {
    super(props);
    this.state = {
      selectedIndex: 0,
      tabList: [
        {
          tabName: 'Telugu',
          tabActiveImage: '',
          tabInactiveImage: '',
          tabActiveText: 'black',
          tabInactiveText: 'gray',
        },
        {
          tabName: 'Tamil',
          tabActiveImage: '',
          tabInactiveImage: '',
          tabActiveText: 'black',
          tabInactiveText: 'gray',
        },
        {
          tabName: 'Hindi',
          tabActiveImage: '',
          tabInactiveImage: '',
          tabActiveText: 'black',
          tabInactiveText: 'gray',
        },
        {
          tabName: 'English',
          tabActiveImage: '',
          tabInactiveImage: '',
          tabActiveText: 'black',
          tabInactiveText: 'gray',
        },
      ],
    };
  }

  onTabItemHandler = index => {
    this.setState({ selectedIndex: index });
  };

  renderBottomContent = () => {
    const { selectedIndex, tabList } = this.state;
    const itemSelected = tabList[selectedIndex];
    switch (itemSelected.tabName) {
      case 'Telugu':
        return (
          <View style={{backgroundColor: 'yellow'}}>
            <Text>Telugu feature will come</Text>
          </View>
        );
      case 'Tamil':
        return (
          <View style={{backgroundColor: 'green'}}>
            <Text>Tamil feature will come</Text>
          </View>
        );
      case 'Hindi':
        return (
          <View style={{backgroundColor: 'cyan'}}>
            <Text>Hindi feature will come</Text>
          </View>
        );
      default:
        return (
          <View>
            <Text>No content</Text>
          </View>
        );
    }
  };

  render() {
    const { tabList, selectedIndex } = this.state;

    return (
      <View style={styles.container}>
        <Text>Some static data loading </Text>
        <View style={styles.tabContainer}>
          {//loop throught the state
          tabList.map((item, index) => {
            return (
              //the style just to make it beautiful and easy to debug
              <View style={styles.tabInnerContainer}>
                <TouchableOpacity
                  style={styles.tabIcons}
                  //this onpress to handle of active selected tab
                  onPress={() => this.onTabItemHandler(index)}>
                  <Image
                    //here's the magic show off
                    source={
                      selectedIndex === index
                        ? require('./assets/snack-icon.png')
                        : undefined
                    }
                    style={{ height: 30, width: 30 }}
                  />
                  <Text
                    style={{
                      color:
                        selectedIndex === index
                          ? item.tabActiveText
                          : item.tabInactiveText,
                    }}>
                    {item.tabName}
                  </Text>
                </TouchableOpacity>
              </View>
            );
          })}
        </View>
        {this.renderBottomContent()}
      </View>
    );
  }

我仍然在这里进行任何澄清或改进。

更新 添加样式:

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'flex-start',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  tabContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
});