【问题标题】:ts(2322) Property children does not exist on type 'Intrinsic attributes and Props'ts(2322) 类型“内在属性和道具”上不存在属性子项
【发布时间】:2020-11-24 20:40:29
【问题描述】:

我在尝试创建一个以“日期”为键的多图时遇到以下错误,然后遍历以数组为值的多图。

输入 '{children: never[];键:字符串;索引:字符串;项目:SomeItem[]; }' 不可分配给类型 'IntrinsicAttributes & Props'。类型“IntrinsicAttributes & Props”上不存在属性“children”。 ts(2322)

不知道如何解决这个问题


   const History = () => {
   
  
   const [counter, setCounter] = useState(0);
   type SomeMap = Map<string,  SomeItem[]>;
   let map: SomeMap = new Map();

    //Items is of type SomeItem[]
    Items.foreach((item) =>{
      if(map.has(item.date)){
       (map.get(item.date) ?? []).push(item);
       } 
      else{
       map.set(item.date,[item]);
       }
      });

      return(
        <Accordian>
         { map.foreach((value, index) => {
           setCounter(counter +1 );
           <Task
            key={index}
            Index={counter.toString()}
            Item={value}>
           </Task>
         })}
        </Accordian>
     );
   
    };

    
   
    type Props = {
     index: string;
     Item: SomeItem[];
    };

    const Task = (props:Props) => {

     const index = props.Index;
     const Item = props.SomeItem;

     
     render(/*Some Code*/);
    };


【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:

    Task 不是用于接收children,但实际上您实际上是在传递一个换行文本节点作为任务的子节点。

          <Task
            key={index}
            Index={counter.toString()}
            Item={value}>{/* there is a new line text node here */}
          </Task>
    

    你可能想让 JSX 标签自动关闭以确保它没有子标签:

          <Task
            key={index}
            Index={counter.toString()}
            Item={value}
          />
    

    Playground

    【讨论】:

    • 很奇怪的错误和错误信息不是吗!
    猜你喜欢
    • 2021-06-03
    • 2022-09-23
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2021-01-11
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多