Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Typescript how send props and use it in child component

I'm using React and TypeScript and trying to pass some data like prop to child component and use it in child component. But i'm getting error and i can't understand why is happened and how fix it. I'm beginner on TypeScript also.

Here is my parent component

import * as React from "react";
import ChildComponent from "./ChildComponent";

const data = [
  {
    title: "A",
    id: 1,
  },
  {
    title: "B",
    id: 1,
  },
];

const ParentComponent = () => {
   return (
      <ChildComponent items={data} />
   )
}

export default ParentComponent;

Here is error in parent component at items

(JSX attribute) items: {
    title: string;
    id: number;
}[]
Type '{ items: { title: string; id: number; }[]; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'items' does not exist on type 'IntrinsicAttributes'.ts(2322)

And when in regular react and es6 i can use this props in child component like this:

const ChildComponent = (props) => {
   return (
      <div>
         {props.items.map((item) => (
           <p to={item.title}></p>
         ))} 
      </div>
   )
}

but have use this props in child component if it will be TypeScript?

like image 962
ciz30 Avatar asked Apr 25 '20 20:04

ciz30


People also ask

Is it possible to use react with typescript?

The React children prop is an important concept for creating reusable components because it allows components to be constructed together. However, a common issue with using React with Typescript is figuring out which data type to use with React children since Typescript is a strongly typed library.

What is a props in react?

Props is simply an abbreviation for properties. In React, we utilize props to send data from one component to another (from a parent component to a child component or multiple children components). They come in handy when you want the data flow in an app to be dynamic. Below is an example of using props to pass data using props.

What is the type of children prop in a class component?

The component type, like the FC type, includes the children prop by default. When we hover over the children prop, we can see what type it has, as seen in the image below: So, the type of the children prop in a class component is ReactNode. Here’s a list of types you might want to utilize in your React application using Typescript.

How do you define props in typescript?

Whether you're coming in fresh to using Typescript with React or are a grizzled veteran looking to add more functional components to your codebase by introducing hooks, it's important to know the different ways to define props. One of the ways you can define props is simply by defining them in the parameter list of a function as demonstrated above.


2 Answers

You need to specify what type of props the child component wants. For example:

interface Item {
  title: string;
  id: number;
}

interface ChildComponentProps {
  items: Item[]
}

const ChildComponent: React.FC<ChildComponentProps> = (props) => {
  return (
    <div>
      {props.items.map((item) => (
        <p to={item.title}></p>
      ))} 
    </div>
  )
}
like image 104
Nicholas Tower Avatar answered Oct 17 '22 05:10

Nicholas Tower


Added to the reply, if the props can be null, you put a question mark.

interface ChildComponentProps {
  items?: Item[]
}
like image 31
tksilicon Avatar answered Oct 17 '22 05:10

tksilicon