Home > Mobile >  Text cannot be used as a jsx component it instance type 'Text' is not a valid JSX element
Text cannot be used as a jsx component it instance type 'Text' is not a valid JSX element

Time:07-23

Receiving following error:

Text cannot be used as a jsx component it instance type 'Text' is not a valid JSX element My file is .tsx as I in a minute need it to be typescript as I will be needing RootState.

I have tried the following so far:

  • Ensured that TS is intalled globally

  • added ' "allowSyntheticDefaultImports" : true ' tsconfig.json

  • devDependencies for typescript is there.

  • dependencies: "react": "17.0.2", "react-dom": "17.0.2",


import * as React from 'react';
import { View, Text} from 'react-native';

export default function ChatScreen({navigation}){
    return(

    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
        <Text
            onPress={() => navigation.navigate('Chat')}
            style={{ fontSize: 26, fontWeight: 'bold'}}>Chat Screen</Text>
    </View>
    
    );
}

Can't seem to understand why I'm not allowed to use JSX components inside my TSX file. Hope one can help understand.

CodePudding user response:

you can add <React.Fragment> :

return(
   <React.Fragment>
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
        <Text
            onPress={() => navigation.navigate('Chat')}
            style={{ fontSize: 26, fontWeight: 'bold'}}>Chat Screen</Text>
    </View>
    </React.Fragment>
    );

here for more info

CodePudding user response:

Add the following to your package.json

"resolutions": {
  "@types/react": "^17.0.38"
}
  • Related