The issue is that your syntax is invalid, you should have something like this :
var links = [
{ endpoint: '/america' },
{ endpoint: '/canada' },
{ endpoint: '/norway' },
{ endpoint: '/bahamas' }
];
class Navigation extends React.Component {
render() {
const listItems = links.map((link) =>
<li key={link.endpoint}>{link.endpoint}</li>
);
return (
<div className="navigation">
<ul>
{listItems}
</ul>
</div>
);
}
Answer from Daniel Andrei on Stack OverflowThe issue is that your syntax is invalid, you should have something like this :
var links = [
{ endpoint: '/america' },
{ endpoint: '/canada' },
{ endpoint: '/norway' },
{ endpoint: '/bahamas' }
];
class Navigation extends React.Component {
render() {
const listItems = links.map((link) =>
<li key={link.endpoint}>{link.endpoint}</li>
);
return (
<div className="navigation">
<ul>
{listItems}
</ul>
</div>
);
}
You should be able to do something like this:
class Navigation extends React.Component {
render() {
return (
<div className="navigation">
<ul>
{
links.map(link =>
<li key={link.endpoint}>{link.endpoint}</li>
)
}
</ul>
</div>
);
}
React iterate through object nested in array
TypeScript + React.js -- how do you iterate over an array of objects with map() in order to render content?
reactjs - How do i loop through an array of objects in react - Stack Overflow
Loop through an array of objects and compare values. If value is not found, execute something else.
Videos
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?
you can use this code
const Todos = ({ todos }) => {
return (
<>
<Head>
<title>To Do List</title>
</Head>
<div>
<h1>To Do List</h1>
{todos && todos.length>0 && todos.map((todoList,index) => (
{index < 50 && <div key={todoList.id}>
<a><h3>
{ todoList.title}
</h3> </a>
</div>}
))}
</div>
</>
);
}
you must handle your array
const Todos = ({ todos }) => {
return (
<>
<Head>
<title>To Do List</title>
</Head>
<div>
<h1>To Do List</h1>
{todos && todos.length>0 && todos.map(todoList => (
<div key={todoList.id}>
<a><h3>
{ todoList.title}
</h3> </a>
</div>
))}
</div>
</>
);
}