TypeScript 在React中点击FC的命令不再起作用,

z4bn682m  于 2个月前  发布在  TypeScript
关注(0)|答案(9)|浏览(50)

这个问题是否在所有扩展都被禁用时发生?:是/否

  • VS Code 版本:1.87.2
  • OS 版本:macOS 14.1.1

重现步骤:

  1. 打开一个带有功能组件的 react 项目
  2. 从代码中使用 CMD 点击一个功能组件
  3. 它没有打开组件,而是打开了 FC 在 react 中的定义
qaxu7uf2

qaxu7uf21#

请分享一个最小的、自包含的代码示例。

vql8enpb

vql8enpb2#

// Component.ts
import { FC } from "react";

export const Component: FC = () => <span>Hello World</span>;

// App.ts
import { Component } from "./Component";
import { FC } from "react";

export const App: FC = () => <Component />;

export default App;

now cmd click Component inside App.ts and you land on line 1098 of file react/node_modules/@types/react/index.d.ts
where the code is

// ...

    interface FunctionComponent<P = {}> {
        (props: P, context?: any): ReactNode;
     
// ...
bz4sfanl

bz4sfanl3#

这并不总是发生。我无法说出这两者之间的区别,但其中一个有效而另一个无效:

$x_1a^0b^1x$

kg7wmglp

kg7wmglp4#

添加这也发生在我身上,如果你定义了一个用forwardRefmemo Package 的组件。
快速演示:
vscode-bug-maybe.mov

function NormalComponent() {
	return <div />;
}

const FCComponent: React.FC = () => {
	return <div />;
};

const MemoComponent = React.memo(() => {
	return <div />;
});

const ForwardRefComponent = React.forwardRef<any, any>((props, ref) => {
	return <div ref={ref} />;
});

function Comp() {
	return (
		<>
			{/* goes to the function definition as expected */}
			<NormalComponent />
			{/* goes to the React type definition :( */}
			<FCComponent />
			<MemoComponent />
			<ForwardRefComponent />
		</>
	);
}
ahy6op9u

ahy6op9u5#

这对我来说似乎非常不一致。我正在使用mobx状态树,但它也发生在非观察组件上,有时并不发生在观察组件上。

s4chpxco

s4chpxco6#

对于我来说,情况并非如此,正如@yspreen所指出的,这种情况似乎并不一致。我将editor.preferGoToSourceDefinition切换为true,似乎有时会产生差异。

g9icjywg

g9icjywg7#

我无法复现这些情况,要么是因为信息不足,要么是因为展示的问题(例如第二次点击MemoComponent)没有发生。

s3fp2yjn

s3fp2yjn8#

我可以提供哪些信息来帮助复现问题?

ruyhziif

ruyhziif9#

最好的情况是我们可以克隆一个仓库,再附上一套说明文档,展示问题所在。

相关问题