简体   繁体   English

不能在 react native 上用作 JSX 组件

[英]cannot be used as a JSX component on react native

I use Conditional Rendering to render which view that would be appier in the page,...我使用条件渲染来渲染页面中更适合的视图,...

const NewSales = () => {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>This is New Sales !</Text>
      </View>
    );
  };

 const TransactionViewDetail = ({ transactionType }: { transactionType: TransactionType }) => {
    if (transactionType == 'New Sales') {
      return <NewSales />;
    }
  };

 return (
    <>
      <StatusBar backgroundColor="transparent" barStyle="light-content" translucent={true} />
      <View style={{ flexGrow: 1, backgroundColor: colors.whiteSmoke }}>
        <TransactionViewDetail transactionType={route.params.type} />
      </View>
    </>
  );

but i got error on <TransactionViewDetail/> .但我在<TransactionViewDetail/>上遇到错误。 The error is 'TransactionViewDetail' cannot be used as a JSX component.错误是“TransactionViewDetail”不能用作 JSX 组件。 Its return type 'Element |它的返回类型'元素 | undefined' is not a valid JSX element.Type 'undefined' is not assignable to type 'Element | undefined' 不是有效的 JSX 元素。类型 'undefined' 不能分配给类型 'Element | null'.无效的'。

Note that React Components must not be declared inside another component.请注意,不得在另一个组件中声明 React 组件。 There are two things that you can do here.您可以在这里做两件事。

1st thing is you can call your functions inside your JSX like this:第一件事是你可以像这样在 JSX 中调用你的函数:

const NewSales = () => {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>This is New Sales !</Text>
      </View>
    );
  };

 const TransactionViewDetail = ({ transactionType }: { transactionType: TransactionType }) => {
    if (transactionType == 'New Sales') {
      return NewSales;
    }
  };

 return (
    <>
      <StatusBar backgroundColor="transparent" barStyle="light-content" translucent={true} />
      <View style={{ flexGrow: 1, backgroundColor: colors.whiteSmoke }}>
        {TransactionViewDetail({ transactionType: route.params.type })}
      </View>
    </>
  );

2nd thing is you can make components on root level (not inside another component) like this:第二件事是您可以像这样在根级别(而不是在另一个组件内)制作组件:

const NewSales = () => {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>This is New Sales !</Text>
      </View>
    );
  };

const TransactionViewDetail = ({ transactionType }: { transactionType: TransactionType }) => {
    if (transactionType == 'New Sales') {
      return <NewSales />;
    }
  };

const YOUR_COMPONENT = () => {
   return (
    <>
      <StatusBar backgroundColor="transparent" barStyle="light-content" translucent={true} />
      <View style={{ flexGrow: 1, backgroundColor: colors.whiteSmoke }}>
        <TransactionViewDetail transactionType={route.params.type} />
      </View>
    </>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM