“类型 '{ children: Element[]; pictureUrl: string; }' 不可分配给类型 'IntrinsicAttributes & { children?: ReactNode; }'
"Type '{ children: Element[]; pictureUrl: string; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'
我在使用 Typescript 和以下消息时遇到问题:
{ children: 元素[];图片网址:字符串; }' 不可分配给类型 'IntrinsicAttributes & { children?: ReactNode; }'
属性 'pictureUrl' 在类型 'IntrinsicAttributes & { children?: ReactNode; }'
上不存在
我真的不明白我做错了什么,有人看到问题了吗?
这是我的 App 组件:
import React from 'react';
import { Root, Routes, addPrefetchExcludes } from 'react-static';
import { Link, Router } from '@reach/router';
import FancyDiv from 'components/FancyDiv';
import Dynamic from 'containers/Dynamic';
import './app.css';
import NavDomicile from './components/NavDomicile';
// Any routes that start with 'dynamic' will be treated as non-static routes
addPrefetchExcludes([ 'dynamic' ]);
function App() {
return (
<Root>
<NavDomicile pictureUrl="./assets/logo-lavigo-domicile.png">
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/blog">Blog</Link>
<Link to="/dynamic">Dynamic</Link>
</NavDomicile>
<div className="content">
<FancyDiv>
<React.Suspense fallback={<em>Loading...</em>}>
<Router>
<Dynamic path="dynamic" />
<Routes path="*" />
</Router>
</React.Suspense>
</FancyDiv>
</div>
</Root>
);
}
export default App;
And My Nav Component :
<!-- begin snippet: js hide: false console: true babel: false -->
import React, { ReactNode } from 'react';
interface INavProps {
pictureUrl?: string;
children?: ReactNode;
}
const NavDomicile: React.FC = ({ children, pictureUrl }: INavProps) => {
return (
<nav>
<div>
<img src={pictureUrl} />
</div>
<div>{children}</div>
</nav>
);
};
export default NavDomicile;
您应该为 React.FC
的类型泛型提供接口
const NavDomicile: React.FC<INavProps> = ({ children, pictureUrl }) => {
}
我在使用 Typescript 和以下消息时遇到问题:
{ children: 元素[];图片网址:字符串; }' 不可分配给类型 'IntrinsicAttributes & { children?: ReactNode; }'
属性 'pictureUrl' 在类型 'IntrinsicAttributes & { children?: ReactNode; }'
上不存在我真的不明白我做错了什么,有人看到问题了吗?
这是我的 App 组件:
import React from 'react';
import { Root, Routes, addPrefetchExcludes } from 'react-static';
import { Link, Router } from '@reach/router';
import FancyDiv from 'components/FancyDiv';
import Dynamic from 'containers/Dynamic';
import './app.css';
import NavDomicile from './components/NavDomicile';
// Any routes that start with 'dynamic' will be treated as non-static routes
addPrefetchExcludes([ 'dynamic' ]);
function App() {
return (
<Root>
<NavDomicile pictureUrl="./assets/logo-lavigo-domicile.png">
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/blog">Blog</Link>
<Link to="/dynamic">Dynamic</Link>
</NavDomicile>
<div className="content">
<FancyDiv>
<React.Suspense fallback={<em>Loading...</em>}>
<Router>
<Dynamic path="dynamic" />
<Routes path="*" />
</Router>
</React.Suspense>
</FancyDiv>
</div>
</Root>
);
}
export default App;
And My Nav Component :
<!-- begin snippet: js hide: false console: true babel: false -->
import React, { ReactNode } from 'react';
interface INavProps {
pictureUrl?: string;
children?: ReactNode;
}
const NavDomicile: React.FC = ({ children, pictureUrl }: INavProps) => {
return (
<nav>
<div>
<img src={pictureUrl} />
</div>
<div>{children}</div>
</nav>
);
};
export default NavDomicile;
您应该为 React.FC
const NavDomicile: React.FC<INavProps> = ({ children, pictureUrl }) => {
}