Reactjs 如何将'props'传递给TypeScript中的另一个组件
我有这个部分:Reactjs 如何将'props'传递给TypeScript中的另一个组件,reactjs,typescript,react-props,Reactjs,Typescript,React Props,我有这个部分: interface listvote { items: any[] } const ListVote: React.FC<listvote> = props => { useEffect(() => { console.log(items) }) return ( <VoteResult id={props.id} img={props.img} name={props.name
interface listvote {
items: any[]
}
const ListVote: React.FC<listvote> = props => {
useEffect(() => {
console.log(items)
})
return (
<VoteResult
id={props.id}
img={props.img}
name={props.name}
score={props.score}
/>
)
}
export default ListVote
const dataListVote = [
{
id: 1,
img:
'sampleimage.com',
name: 'samplename',
score: 50,
},
{
id: 2,
img:
'sampleimage.com',
name: 'samplename',
score: 80,
},
interface listvote {
items: any[]
}
const VoteResult: React.FC<voteresult> = props => {
useEffect(() => {
console.log(props)
})
return <h1>hello</h1>
}
export default VoteResult
接口列表{
项目:任何[]
}
const ListVote:React.FC=props=>{
useffect(()=>{
console.log(项目)
})
返回(
)
}
导出默认列表投票
它将此阵列作为道具接收:
<ListVote items={dataListVote} />
const dataListVote = [
{
id: 1,
img:
'sampleimage.com',
name: 'samplename',
score: 50,
},
{
id: 2,
img:
'sampleimage.com',
name: 'samplename',
score: 80,
},
]
常量dataListVote=[
{
id:1,
img:
"sampleimage.com",,
名称:“samplename”,
分数:50,,
},
{
id:2,
img:
"sampleimage.com",,
名称:“samplename”,
得分:80分,
},
]
ListVote中还有另一个组件:
interface voteresult {
items: any[]
}
const VoteResult: React.FC<voteresult> = props => {
useEffect(() => {
console.log(props)
})
return <h1>hello</h1>
}
export default VoteResult
接口voteresult{
项目:任何[]
}
const VoteResult:React.FC=props=>{
useffect(()=>{
控制台日志(道具)
})
打招呼
}
导出默认VoteResult
问题是,当我尝试将同一数组传递给ListVote组件中的另一个组件时,它会抛出以下错误:
Type 'PropsWithChildren<listvote>' is missing the following properties from type 'any[]': length, pop, push, concat, and 28 more.
类型“PropsWithChildren”缺少类型“any[]”中的以下属性:长度、弹出、推送、concat等。
根据您的使用情况:
<VoteResult
id={props.id}
img={props.img}
name={props.name}
score={props.score}
/>
正确类型
要消除此错误,最简单的类型是:
interface voteresult {
id: any;
img: any;
name: any;
score: any;
}
主组件:
interface listvote {
items: any[]
}
const ListVote: React.FC<listvote> = props => {
useEffect(() => {
console.log(items)
})
return (
<VoteResult
id={props.id}
img={props.img}
name={props.name}
score={props.score}
/>
)
}
export default ListVote
const dataListVote = [
{
id: 1,
img:
'sampleimage.com',
name: 'samplename',
score: 50,
},
{
id: 2,
img:
'sampleimage.com',
name: 'samplename',
score: 80,
},
interface listvote {
items: any[]
}
const VoteResult: React.FC<voteresult> = props => {
useEffect(() => {
console.log(props)
})
return <h1>hello</h1>
}
export default VoteResult
]
列表投票组件:
interface listvote {
items: any[]
}
const ListVote: React.FC<listvote> = props => {
useEffect(() => {
console.log(typeof props)
})
return <VoteResult items={props} />
}
export default ListVote
接口列表{
项目:任何[]
}
const ListVote:React.FC=props=>{
useffect(()=>{
控制台日志(道具类型)
})
返回
}
导出默认列表投票
VoteResult组件:
interface listvote {
items: any[]
}
const ListVote: React.FC<listvote> = props => {
useEffect(() => {
console.log(items)
})
return (
<VoteResult
id={props.id}
img={props.img}
name={props.name}
score={props.score}
/>
)
}
export default ListVote
const dataListVote = [
{
id: 1,
img:
'sampleimage.com',
name: 'samplename',
score: 50,
},
{
id: 2,
img:
'sampleimage.com',
name: 'samplename',
score: 80,
},
interface listvote {
items: any[]
}
const VoteResult: React.FC<voteresult> = props => {
useEffect(() => {
console.log(props)
})
return <h1>hello</h1>
}
export default VoteResult
接口列表{
项目:任何[]
}
const VoteResult:React.FC=props=>{
useffect(()=>{
控制台日志(道具)
})
打招呼
}
导出默认VoteResult
错误:
Type 'PropsWithChildren<listvote>' is missing the following properties from type 'any[]': length, pop, push, concat, and 28 more.
类型“PropsWithChildren”缺少类型“any[]”中的以下属性:长度、弹出、推送、concat等。
更改一个问题,但仍然出现错误请参见第二篇文章