由于某些错误,React-Native Flatlist 无法渲染
2018-12-22
185
Flatlist 已实现,但数据无法正确呈现
App.js
import React, {Component} from 'react';
import Main from './components/MainComponents';
import {Platform} from 'react-native';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
export default class App extends Component<Props> {
render() {
return (
<Main />
);
}
}
MainComponent.js
import React,{Component} from 'react';
import {Menu} from './MenuComponent';
import { DISHES } from '../shared/dishes';
class Main extends Component{
constructor(props){
super(props);
this.state={
dishes: DISHES
}
}
render(){
return(
<Menu dishes={this.state.dishes} />
)
}
}
export default Main
MenuComponent.js
import React,{Component} from 'react';
import {View,Flatlist } from 'react-native';
import {ListItem} from 'react-native-elements';
function Menu(props){
const renderMenuItem=({item,index}) => {
return(
<ListItem
key={index}
title={item.name}
subtitle={item.description}
hideChevron={true}
leftAvatar={{ source: require('./images/images/buffet.png')}}
/>
)
}
return(
<Flatlist
data={props.dishes}
renderItem={renderMenuItem}
keyExtractor={item => item.id.toString()}
/>
)
}
export default Menu
错误
您可能忘记导出组件,或者您混淆了默认导入和命名导入
我已阅读所有相关问题,但对我不起作用 react-native 新手
非常感谢您的帮助
谢谢
1个回答
由于 Menu 组件是默认导出的,因此您无法像 {Menu} 那样导入它,因此
在您的 MainComponent.js 中
将
import {Menu} from './MenuComponent';
更改为
import Menu from './MenuComponent';
同时
将
import Main from './components/MainComponents';
更改为
import Main from './components/MainComponent';
Hemadri Dasari
2018-12-22