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;
}
Answer from Hakim Abdelcadir on Stack OverflowYou 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
React - Pass an Array as Props
Reactjs/Typescript cannot pass array of items as property to child
React - Pass an Array as Props
Passing arrays from one component to another, using props.
Videos
Hello!
I'm taking my first steps into React -and JV- and encounter a problem I can't solve. It looks like one of those that are actually incredibly simple, the answer just in front of my eyes. But, dammit, I can't see it.
With create-react-app I've been messing with the App.js file. I'm trying to define an Array of objects, and then pass those objects to child functions.
App function:
import React from 'react'
const App = () => {
const parts = [
{
name: 'This is part 1',
exercises: 20
},
{
name: 'Part two it is',
exercises: 5
},
{
name: 'Threeee',
exercises: 16
}
]
console.log(parts) //output array: (3) [{…}, {…}, {…}]
console.log(parts[0]) //output object:{name: "This is part 1", exercises: 20}
return (
<div>
<Content parts={parts} />
</div>
)
}Content function:
const Content = (props) => {
console.log(props) //output object!?: {parts: Array(3)}
console.log(props[0]) //output: undefined
return (
<>
<Part1 props={props[0]} />
<Part2 props={props[1]} />
<Part3 props={props[2]} />
</>
)
}part1 function (part2 and part3 are the same as this):
const Part1 = (props2) => {
console.log(props2) //output object!?: {props: undefined}
return (
<>
<p>
{props2.name} {props2.exercises}
</p>
</>
)
}What I need to accomplish for the exercise is to only pass the array parts in App function like this: <Content parts={parts} />
Then Content function should use the object indexed 0 on the array and pass it to Part1, which should return the <p> code.
I guess there's an error when passing the array to Content. It gets converted to an object and then I cant use the index method. If I try props[0] I get nothing for it is undefined. If I try to console.log(props[0].name) I get the error: TypeError: Cannot read property 'name' of undefined
How can I pass the array to the child functions? I know the structure of the app might not be ideal, but it's exercise 1.4 for the Fullstackopen Course, by the University of Helsinki. I changed the actual content and eliminated some code to avoid trouble.
I will appreciate any help with the code and with the way I presented my problem, I also have to learn how to do that! Thanks!
The curly braces only need to be used within JSX elements. Like this:
<MyComponent somProp={['something']} />
In the case above, the {} is the way of saying: "Evaluate the expression that I passed within and pass it as a prop". Within the {} you can pass any valid JavaScript object or expression. Note that if you pass a string, and specifically for strings, you don't need the curly braces... like <MyComponent somProp="something" />.
The above code is the equivalent of this:
var myArray = ['something'];
<MyComponent somProp={myArray} />
You actually don't need to specify PropTypes at all to use props. It's just a good way to document and verify prop types during development.
You are using the {} correctly. {} will return the value of the expression inside.
However, {['everyone']} doesn't make much sense. Here you are asking React to return the value of the array itself, rather than one of the elements/values within the array.
To get the first value out of your array, you should be doing: {this.props.config[0]} since the value "everyone" is at the 0 index of the array.
If your array had multiple values, you would do something along the lines of:
render: function() {
var values = this.props.config.map(function(value, i){
return (
<p>value</p>
);
});
return (
<div className="navigation">
helloworld {values};
</div>
);
}
If you truly to mean to actually print out the array itself, and not a particular value within it, you have two good options:
render: function() {
return (
<div className="navigation">
helloworld {this.props.config.toString()};
</div>
);
}
Or
render: function() {
return (
<div className="navigation">
helloworld {JSON.stringify(this.props.config)};
</div>
);
}