1
I’m creating an React app with Typescript that searches the Github API.
I’m having trouble passing one array as props to another component.
The logic would be: User
has Repos
(array of Github repositories), which in turn has RepoItem
(individual object of array).
But I’m getting the following error:
JSX element type 'Element[]' is not a constructor function for JSX elements. Type 'Element[]' is missing the following properties from type 'Element': type, props, key TS2605
Filing cabinet App.tsx
:
return (
<User
user={user}
repos={repos}
loading={loading}
/>
);
Filing cabinet User.tsx
:
The error occurs specifically in the following excerpt: <Repos repos={repos} />
, in the method render
of the component User
.
import React, { Component, Fragment } from "react";
import Repos from "../repos/Repos";
import { RouteComponentProps } from "react-router";
interface IRepos {
id: string;
name: string;
html_url: string;
}
interface IUser {
user: any;
loading: boolean;
repos: IRepos[];
}
class User extends Component<IUser & RouteComponentProps> {
render() {
const { loading, repos } = this.props;
if (loading) {
return <Spinner />;
} else {
return (
<Fragment>
<Repos repos={repos} />
</Fragment>
);
}
}
}
export default User;
Filing cabinet Repos.tsx
:
import React from "react";
import PropTypes from "prop-types";
import RepoItem from "./RepoItem";
interface IRepos {
id: string;
name: string;
html_url: string;
}
interface IRepoWrapper {
repos: IRepos[];
}
const Repos = ({ repos }: IRepoWrapper) => {
return repos.map(repo => <RepoItem repo={repo} key={repo.id} />);
};
Repos.propTypes = {
repos: PropTypes.array.isRequired
};
export default Repos;
Filing cabinet RepoItem.tsx
:
import React from "react";
import PropTypes from "prop-types";
interface IRepos {
repo: {
id: String;
name: String;
html_url: string;
};
}
const RepoItem = ({ repo }: IRepos) => {
return (
<div className='card'>
<h3>
<a href={repo.html_url}>{repo.name}</a>
</h3>
</div>
);
};
RepoItem.propTypes = {
repo: PropTypes.object.isRequired
};
export default RepoItem;
I did a test bringing RepoItem
straight to User
, and then using map
to return several <RepoItem />
and it worked. But I’d like to do it within Repos
, and not within User
.
Who denied the question could leave a comment on the reason? I would like to improve my question so that it can receive a good answer :)
– user94991
I gave a decrease in the code "useless" and I made clearer where is the error, I hope it helps.
– user94991
I think the problem is the value that component
Repos
is returning. Probably, for some reason, React expects an object (React Element), but is receiving an array. Try to involve the return of this component in aFragment
. Probably solves.– Luiz Felipe
@Luizfelipe
Repos
is returning aarray.map()
right? You want me to encapsulate thearray.map()
within aFragment
? I believe I cannot call my functionmap
within an elementJSX
. I’m not getting it right– user94991
You can use the
map
within JSX yes... For this, use{}
to evaluate expressions within JSX. See the published response now.– Luiz Felipe