Reputation: 1591
I want to map an array of ISO dates and I thought I could to this:
import React from "react";
import "./styles.css";
export default function App() {
const times = ["2021-01-26T17:00:00.000Z", "2021-01-26T17:00:00.000Z"];
const formattedTimes = times.map((time) => new Date(time));
return (
<div className="App">
{formattedTimes.map((time) => (
<h2>{time}</h2>
))}
</div>
);
}
but this causes an error: Here is the sandbox (https://codesandbox.io/s/headless-hill-x18ws?file=/src/App.js:0-346)
"Error: Objects are not valid as a React child (found: Wed Jan 27 2021 01:00:00 GMT+0800 (China Standard Time)). If you meant to render a collection of children, use an array instead."
Upvotes: 2
Views: 136
Reputation: 451
You are trying to loop through formattedTimes
array. But that is the array of objects, so you need to call toString
method.
<div>{
formattedTimes.map(formatted => <span>{formatted.toString()}</span>)
}<div>
Upvotes: 3