简体   繁体   English

JSX 元素类型“字符串”不是 JSX 元素的构造函数 function。 Typescript

[英]JSX element type 'string' is not a constructor function for JSX elements. Typescript

So i have been working on a reusable icon element that I can call in any class and just pass a string <Icon name="chat" /> that contain the specific colour of the icon.. This error is the result of the previous question I raised.. you can find the link below所以我一直在研究一个可重用的图标元素,我可以在任何 class 中调用它并传递一个包含图标特定颜色的字符串<Icon name="chat" /> 。这个错误是上一个问题的结果我提出了..你可以找到下面的链接

I raised a question early before this: 在此之前我很早就提出了一个问题:

icon.ts file图标.ts 文件

const iconsList = {
    heart: '&#xe800;',
    star: '&#xe801;',
    like: '&#xe800;',
    dislike: '&#xe802;',
    flash: '&#xe803;',
    marker: '&#xf031;',
    filter: '&#xf0b0;',
    user: '&#xf061;',
    circle: '&#xf039;',
    hashtag: '&#xf029;',
    calendar: '&#xf4c5;',
    chevronLeft: '&#xf004;',
    optionsV: '&#xf142;',
    optionsH: '&#xf141;',
    chat: '&#xf4ac;',
    explore: '&#xf50d;'
};

interface Props{
    name: keyof typeof iconsList;
}

const Icon = ({name }: Props) => {
    let icon = iconsList[name];
    icon = icon.substr(3);
    icon = String.fromCharCode(parseInt(icon, 16));

    return icon;
 };

export default Icon;

profile.tsx个人资料.tsx

import React from 'react';
import styles from '../assets/styles';

import {
  ScrollView,
  View,
  Text,
  ImageBackground,
  TouchableOpacity
} from 'react-native';
import Icon from '../components/Icon';
const Profile = () => {
return (

<TouchableOpacity>
  <Text style={styles.topIconLeft}>
    <Icon name="chevronLeft" />
  </Text>
</TouchableOpacity>

);
}

This line of <Icon name="chevronLeft" /> complains with an error of "JSX element type 'string' is not a constructor function for JSX elements.ts(2605)这行<Icon name="chevronLeft" />报错“JSX element type 'string' is not a constructor function for JSX elements.ts(2605)

Screenshot:截屏: 在此处输入图像描述

string is not a valid return value for a react component. string不是反应组件的有效返回值。 A react component always has to return a ReactDOM element or a Fragment , that implements the render method.一个 react 组件总是必须返回一个ReactDOM元素或一个Fragment ,它实现了render方法。 You can try你可以试试

const Icon : React.SFC<Props> = ({name }: Props) => {
    let icon = iconsList[name];
    icon = icon.substr(3);
    icon = String.fromCharCode(parseInt(icon, 16));

    return <>{icon}</>;
 };

And actually it won't hurt if you renamed icon.ts to icon.tsx .实际上,如果您将icon.tsx重命名为icon.ts也不会受到伤害。 Depending on your compiler settings the <>{icon}</> part might not get recognized correctly as JSX markup.根据您的编译器设置, <>{icon}</>部分可能无法正确识别为 JSX 标记。

暂无
暂无

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

相关问题 JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; - JSX element type 'ReactElement<any> is not a constructor function for JSX elements. Type 'undefined' is not assignable to type 'Element | null' Ionic React JSX 元素类型 'false | Element' 不是 JSX 元素的构造函数 function。 输入'假 - Ionic React JSX element type 'false | Element' is not a constructor function for JSX elements. Type 'false 打字稿错误 JSX 元素类型“视图”不是 JSX 元素的构造函数 - Typescript error JSX element type 'View' is not a constructor function for JSX elements 在 TypeScript 中使用 JS React 组件:JSX 元素类型“MyComponent”不是 JSX 元素的构造函数 - Using JS React component in TypeScript: JSX element type 'MyComponent' is not a constructor function for JSX elements JSX元素类型&#39;Line&#39;不是JSX元素的构造函数 - JSX element type 'Line' is not a constructor function for JSX elements React:在TS组件中使用ES6组件:TS2605:JSX元素类型&#39;xxx&#39;不是JSX元素的构造函数 - React: Using ES6 component in TS component: TS2605: JSX element type 'xxx' is not a constructor function for JSX elements 用于从字符串创建 JSX 元素的正确 TypeScript 类型 - Proper TypeScript type for creating JSX element from string 反应 typescript 句柄 JSX.Element 或字符串 - React typescript handle JSX.Element or string 用JSX元素替换字符串? - Replace string with JSX element? 如何在 TypeScript React 组件中返回字符串或 JSX 元素? - How to return string or JSX element in TypeScript React component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM