Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ts(2322) Property children does not exist on type 'Intrinsic attributes and Props'

I'm getting the following error while trying to create a multimap with 'date' as the key and then iterate through the multimap which has arrays as it's values.

Type '{children: never[]; key: string; Index: string; Item: SomeItem[]; }' is not assignable to type 'IntrinsicAttributes & Props'. Property 'children' does not exist on type 'IntrinsicAttributes & Props'. ts(2322)

and not sure how to fix this


   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*/);
    };


like image 592
Vinny Avatar asked Aug 04 '20 17:08

Vinny


People also ask

Does not exist children ReactNode?

The React. js error "Property 'children' does not exist on type" occurs when we try access the children property in a component for which we haven't typed the prop. To solve the error, type the children prop in the component as React. ReactNode .

What is IntrinsicAttributes?

IntrinsicAttributes interface can be used to specify extra properties used by the JSX framework which are not generally used by the components' props or arguments - for instance key in React. Specializing further, the generic JSX.

Does not exist on type readonly React?

The React. js error "Property does not exist on type 'Readonly<{}>'" occurs when we try to access the props or state of a class component which we haven't typed. To solve the error, use the generic on the React. Component class to type the props or state objects of the class.

What is ReactNode?

A React node is defined as: a light, stateless, immutable, virtual representation of a DOM node. React nodes are not real DOM nodes (e.g., text or element nodes) themselves, but a representation of a potential DOM node. The representation is considered the virtual DOM.


1 Answers

Task is not typed to receive children, but actually you are actually passing a newline text node as the task's children.

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

You probably want to make the JSX tag self closing to ensure it has no children:

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

Playground

like image 77
Alex Wayne Avatar answered Oct 24 '22 03:10

Alex Wayne