最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React and TypeScript, getting "Type expected" error - Stack Overflow

programmeradmin9浏览0评论

I am converting a React ponent into Typescript (*.ts) from JS. It is giving the error below "Type expected". How would I fix this?

const SisenseDashboard = () => {

  const sisenseRef = React.useRef(null);

  return (
    <>
      <Box
        display="flex"
        flexDirection="column"
        alignItems="center"
        marginBottom={2}
      >
        <h2>In-App Reporting</h2>
      </Box>
      <Box
        ref={sisenseRef}
        id="sisense-container"
        className="sisense-demo"
      ></Box>
    </>
  );
};

I am converting a React ponent into Typescript (*.ts) from JS. It is giving the error below "Type expected". How would I fix this?

const SisenseDashboard = () => {

  const sisenseRef = React.useRef(null);

  return (
    <>
      <Box
        display="flex"
        flexDirection="column"
        alignItems="center"
        marginBottom={2}
      >
        <h2>In-App Reporting</h2>
      </Box>
      <Box
        ref={sisenseRef}
        id="sisense-container"
        className="sisense-demo"
      ></Box>
    </>
  );
};

Share Improve this question edited Jan 29, 2023 at 11:13 Youssouf Oumar 46k16 gold badges100 silver badges104 bronze badges asked Mar 29, 2022 at 17:38 mattsmith5mattsmith5 1,1334 gold badges48 silver badges106 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 23

Make sure your ponent file extension is .tsx and not .ts. This should resolve your issue. Addinonnaly, you could tell TypeScript that your function is a React Functional Component, for example, with the help of the FC type from React:

import {FC} from "react";

const SisenseDashboard : FC = () => {

  const sisenseRef = React.useRef(null);

  return (
    <>
      <Box
        display="flex"
        flexDirection="column"
        alignItems="center"
        marginBottom={2}
      >
        <h2>In-App Reporting</h2>
      </Box>
      <Box
        ref={sisenseRef}
        id="sisense-container"
        className="sisense-demo"
      ></Box>
    </>
  );
};
发布评论

评论列表(0)

  1. 暂无评论