React Native – 元素类型无效

huangapple go评论72阅读模式
英文:

React Native - Element type is invalid

问题

I'm trying to do a React Native app using Expo cli. However when I'm testing it I get error. What should I do to solve the problem?

> Error: Element type is invalid: expected a string (for built-in
> components) or a class/function (for composite components) but got:
> undefined. You likely forgot to export your component from the file
> it's defined in, or you might have mixed up default and named imports.
> Check the render method of FetchExample.

import React, {Component} from 'react';
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { Flatlist, ActivityIndicator, View, TouchableWithoutFeedback } from 'react-native';
import styles from './styles'
import Content from './Components/content';
import ContentDonate from './Components/contentdonate';
import Details from './Components/details';
import Header from './Components/header';
import Footer from './Components/footer';

class FetchExample extends React.Component {
  static navigationOptions = {
    title: 'Database',
  };

  constructor(props){
    super(props);
    this.state ={isLoading: true}
  }

  actionOnRow(item) {
    this.props.navigation.navigate('Details', {item});
  }

  componentDidMount(){
    return fetch('https://mocki.io/v1/01994c7b-9eb6-4fe4-b399-1bc5992d1a10')
    .then((response) => response.json())
    .then((responseJson) => {
      this.setState({
        isLoading: false,
        dataSource: responseJson,
      },
      );
    })
    .catch((error) =>{
      console.error(error);
    });
  }

  render(){
    if(this state.isLoading){
      return(
        <View>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
      <View style={styles.background}>
        <View style={styles.container}>
          <Flatlist
          data ={this.state.dataSource}
          renderItem={({item}) =>
          <TouchableWithoutFeedback onPress ={ () => this.actionOnRow(item)}>
            <Text style = {styles.listTextStyle}>{item.Name}</Text>
          </TouchableWithoutFeedback>
          }
          keyExtractor={item => item.Id.toString()}
          />
        </View>
      </View>
    );
  } 
}

//NAVIGATION
class HomePage extends Component {
  render(){
    return (
      <View style = {styles.container}>
        <Header/>
        <Content/>
        <Footer/>
      </View>
  );
  }
}

class DonatePage extends Component {
  render(){
    return (
      <View style = {styles.container}>
        <Header/>
        <ContentDonate/>
        <Footer/>
      </View>
  );
  }
}

const AppNavigator = createStackNavigator(
  {
    Home: HomePage,
    Donate: DonatePage,
    Details: Details,
    Database: FetchExample
  },
  {
    initialRouteName: 'Home',
  }
);

export default createAppContainer(AppNavigator);
英文:

I'm trying to do a React Native app using Expo cli. However when I'm testing it I get error. What should I do to solve the problem?

> Error: Element type is invalid: expected a string (for built-in
> components) or a class/function (for composite components) but got:
> undefined. You likely forgot to export your component from the file
> it's defined in, or you might have mixed up default and named imports.
> Check the render method of FetchExample.

import React, {Component} from &#39;react&#39;;
import { createAppContainer } from &#39;react-navigation&#39;;
import { createStackNavigator } from &#39;react-navigation-stack&#39;;
import { Flatlist, ActivityIndicator, View, TouchableWithoutFeedback } from &#39;react-native&#39;;
import styles from &#39;./styles&#39;
import Content from &#39;./Components/content&#39;
import ContentDonate from &#39;./Components/contentdonate&#39;;
import Details from &#39;./Components/details&#39;;
import Header from &#39;./Components/header&#39;;
import Footer from &#39;./Components/footer&#39;
class FetchExample extends React.Component {
static navigationOptions = {
title: &#39;Database&#39;,
};
constructor(props){
super(props);
this.state ={isLoading: true}
}
actionOnRow(item) {
this.props.navigation.navigate(&#39;Details&#39;, {item});
}
componentDidMount(){
return fetch(&#39;https://mocki.io/v1/01994c7b-9eb6-4fe4-b399-1bc5992d1a10&#39;)
.then((response) =&gt; response.json())
.then((responseJson) =&gt; {
this.setState({
isLoading: false,
dataSource: responseJson,
},
);
})
.catch((error) =&gt;{
console.error(error);
});
}
render(){
if(this.state.isLoading){
return(
&lt;View&gt;
&lt;ActivityIndicator/&gt;
&lt;/View&gt;
)
}
return(
&lt;View style={styles.background}&gt;
&lt;View style={styles.container}&gt;
&lt;Flatlist
data ={this.state.dataSource}
renderItem={({item}) =&gt;
&lt;TouchableWithoutFeedback onPress ={ () =&gt; this.actionOnRow(item)}&gt;
&lt;Text style = {styles.listTextStyle}&gt;{item.Name}&lt;/Text&gt;
&lt;/TouchableWithoutFeedback&gt;
}
keyExtractor={item =&gt; item.Id.toString()}
/&gt;
&lt;/View&gt;
&lt;/View&gt;
);
} 
}
//NAVIGATION
class HomePage extends Component {
render(){
return (
&lt;View style = {styles.container}&gt;
&lt;Header/&gt;
&lt;Content/&gt;
&lt;Footer/&gt;
&lt;/View&gt;
);
}
}
class DonatePage extends Component {
render(){
return (
&lt;View style = {styles.container}&gt;
&lt;Header/&gt;
&lt;ContentDonate/&gt;
&lt;Footer/&gt;
&lt;/View&gt;
);
}
}
const AppNavigator = createStackNavigator(
{
Home: HomePage,
Donate: DonatePage,
Details: Details,
Database: FetchExample
},
{
initialRouteName: &#39;Home&#39;,
}
);
export default createAppContainer(AppNavigator);

答案1

得分: 1

  • 语法错误:Flatlist,更改为 FlatList
  • Id 字段在 keyExtractor 中找不到,请将 keyExtractor={(item) => item.Id.toString()} 更改为 keyExtractor={(item) => item._id}_id 已经是一个字符串)。
英文:
  • Syntax Error: Flatlist, change it to FlatList.
  • Id field not found in keyExtractor, change keyExtractor={(item) =&gt; item.Id.toString()} to keyExtractor={(item) =&gt; item._id} (_id is already a string)

huangapple
  • 本文由 发表于 2023年2月16日 16:12:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定