The main issue is that you're doing React.FC<[Props]> instead of React.FC<Props>. With the square brackets, you're creating a tuple type, whose's zeroth element is of type Props, and then you're having that tuple be the props of your component.
interface Props {
propWhichIsArray: {
id: ID; // I assume ID is defined elsewhere
text: string;
}[]
}
const Component: React.FC<Props> = ({ propWhichIsArray }) => {
If this data in the array is being used in other places, you may want to pull it out to its own interface:
interface Thingamajig {
id: ID;
text: string;
}
interface Props {
propWhichIsArray: Thingamajig[];
}
Answer from Nicholas Tower on Stack Overflowreactjs - React.Js - Typescript how to pass an array of Objects as props? - Stack Overflow
How to pass an array as a prop in typescript react hooks
TypeScript + React.js -- how do you iterate over an array of objects with map() in order to render content?
reactjs - React js Typescript string array variable - Stack Overflow
What is the syntax for an array of objects in TypeScript?
How do you push an object into a TypeScript array?
What is the best way to define an array of objects in TypeScript?
Videos
You are better off using a native array instead of an object literal with number-like properties, so that numbering (as well as numerous other array functions) are taken care of off-the-shelf.
What you are looking for here is an inline interface definition for your array that defines every element in that array, whether initially present or introduced later:
let userTestStatus: { id: number, name: string }[] = [
{ "id": 0, "name": "Available" },
{ "id": 1, "name": "Ready" },
{ "id": 2, "name": "Started" }
];
userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]
If you are initializing your array with values right away, the explicit type definition is not a necessity; TypeScript can automatically infer most element types from the initial assignment:
let userTestStatus = [
{ "id": 0, "name": "Available" },
...
];
userTestStatus[34978].nammme; // Error: Property 'nammme' does not exist on type [...]
What you have above is an object, not an array.
To make an array use [ & ] to surround your objects.
userTestStatus = [
{ "id": 0, "name": "Available" },
{ "id": 1, "name": "Ready" },
{ "id": 2, "name": "Started" }
];
Aside from that TypeScript is a superset of JavaScript so whatever is valid JavaScript will be valid TypeScript so no other changes are needed.
Feedback clarification from OP... in need of a definition for the model posted
You can use the types defined here to represent your object model:
type MyType = {
id: number;
name: string;
}
type MyGroupType = {
[key:string]: MyType;
}
var obj: MyGroupType = {
"0": { "id": 0, "name": "Available" },
"1": { "id": 1, "name": "Ready" },
"2": { "id": 2, "name": "Started" }
};
// or if you make it an array
var arr: MyType[] = [
{ "id": 0, "name": "Available" },
{ "id": 1, "name": "Ready" },
{ "id": 2, "name": "Started" }
];
You can simply return this in the User component:
return (
<UsersList users={USERS} />
)
Then the UsersList component will become:
const UsersList = ( { users } : UsersListProps) => {
if(!users?.length){
return(
<div>
<h1>No users found</h1>
</div>
);
}
return(
<ul>
{users.map(({ id, name, image, places, placeCount }) => (
<UserIListtem
key={id}
name={name}
id={id}
image={image}
places={places}
placeCount={placeCount}/>
))}
</ul>
);
}
And the UserIListtem component:
const UserIListtem = ({ name: user, id, image, placeCount, places: placeName }: User) => {
return (
<li>
<div>
<div>
<img src={image} alt={placeName}/>
</div>
<div>{id}
<h2>{user}</h2>
<h3>{placeCount}</h3>
</div>
</div>
</li>
)
}
Remember to also change the UsersListProps interface to:
export interface UsersListProps {
users: User[];
}
export interface User {
id: number;
name: string;
image: string;
placeCount: number;
places: string;
}
You can just replace props with users: Array and then send the whole array and load the data from there in your UserIListtem.
I have difficulty passing an array as a prop to a component from the parent in react typescript.
The error I am getting is
Type '{ report:ReportData[]; }' is not assignable to type 'IntrinsicAttributes & ReportData & { children?: ReactNode; }'.
Property 'report' does not exist on type 'IntrinsicAttributes & ReportData & { children?: ReactNode; }'. Did you mean 'Port'?
import ReportComponent from '../Components/Reports/ReportComponent';
import { ReportData } from "../Types/ReportData.types";
const Report = () => {
const [Report, setReport] = useState<ReportData[]>([]);
ReportService.GetReport()
.then((response) => {
console.log(response.data.data);
setReport(response.data.data);
toast.success(response.data.message);
}).catch((e) => {
console.log(e);
});
return <ReportComponent report={Report}/>;//ReportComponent
const ReportComponent: React.FC<ReportData> = (props:ReportData) => {
console.log("props",props)
return <div className="row">
</div>
}I will appreciate help on fixing this
Thanks
Hi, I have not been able to get any sort of clarity on this. Maybe someone here can help.
So, I need to fetch an API and return a list of products in a store, and then render that out to the user. In regular React this is easy, but I started learning TypeScript and it is throwing me for a loop.
I have the following interfaces + useState:
interface ProductInterface {
id: string;
fields: {
company: string;
featured: boolean;
name: string;
price: number;
}
}
interface ProductsArrayInterface {
products: ProductInterface[];
}
const [products, setProducts] = useState<ProductsArrayInterface|null>(null);
After calling the API I do setProducts(products) and then try to render it like so:
{products && products.map((product:any) => {
const {id, fields} = product;
return (
<article key={id}>
<h4>{fields.name}</h4>
<h5>${fields.price/100}</h5>
</article>
)
})
}
However, the error I get is "Property 'map' does not exist on type 'ProductsArrayInterface'". I tried using products.products.map() instead, and while that quieted the compiler errors, on run time the page doesn't load and it gives the "cannot read properties of undefined (reading 'map')" error in the console. So I am not sure what to do. What should I be doing here to render the array of objects?