0

This is my code in "App.js" as the array I want to loop from, I wanted to loop from the array below and output the contents of it in the browser with a table.

The array

This is my return

    return (
    
    <div className="container">
      <h1> Expenses </h1>

      <table className="table table-bordered">
        <tr>
          <th>Title</th>
          <th>Amount</th>
          <th>Date</th>
        </tr>

        {expenses.map((expense, index) => (
          <tr data-index={index}>
            <td>{expense.title}</td>
            <td>{expense.amount}</td>
            <td>{expense.date}</td>
          </tr>
        ))}

      </table>

    </div>
  );

This is the code from my component.

import './ExpenseItem.css';

function ExpenseItem(props) {
    return (
        <div className="expense-item" >
            <div>{props.date.toISOString()}</div>
            <div className="expense-item__description">
                <h2>{props.title}</h2>
                <div className="expense-item__price">{props.amount}</div>
            </div>
        </div>
    )
}

export default ExpenseItem

This is the error that I am getting. Error I am getting in console

4 Answers 4

3

You need to convert dates to strings. In your code, you pass Date Objects to HTML. The data format should be String. You can see formatting options for dates on MDN.

<td>{expense.date.toString()}</td>
Sign up to request clarification or add additional context in comments.

3 Comments

It's working now, thank you! I was just wondering, I already declared it in my props, but it didn't work still?
<div>{props.date.toISOString()}</div>
If you mean props.date.toISOString(), it doesn't change/update prop. It is just printing the formatted data but the prop stays the same.
1

You're trying to output a Date object:

<td>{expense.date}</td>

Which, as the error states, is an object and not a simple value. So React has no way of knowing how to display it.

You can define the format and output that. For example:

<td>{expense.date.toLocaleDateString()}</td>

There are a variety of functions built-in to the Date object to format it, or you can combine multiple values from it into a custom format, or use a date formatting/parsing library, etc.

6 Comments

It's working now, thank you! I was just wondering, I already declared it in my props, but it didn't work still? <div>{props.date.toISOString()}</div>
@PhilippeMallari: It's not clear what you're asking in that comment. That line of code is an example of formatting a date value for output. Is that not working in some way? Why don't you apply that same approach to the date value which is causing the error?
Yes, it is actually in my component already, I just thought that it would already be handled there as this was my code in the component "ExpenseItem.js"
import './ExpenseItem.css'; function ExpenseItem(props) { return ( <div className="expense-item" > <div>{props.date.toISOString()}</div> <div className="expense-item__description"> <h2>{props.title}</h2> <div className="expense-item__price">{props.amount}</div> </div> </div> ) } export default ExpenseItem
Either way it's working, thank you!
|
0
return (
    
    <div className="container">
      <h1> Expenses </h1>

      <table className="table table-bordered">
        <tr>
          <th>Title</th>
          <th>Amount</th>
          <th>Date</th>
        </tr>

        {expenses.map((expense, index) => (
          <tr data-index={index}>
            <td>{expense.title}</td>
            <td>{expense.amount}</td>
            <td>{expense.date.toISOString()}</td>
          </tr>
        ))}

      </table>

    </div>
  );

Date object is not valid react child.

1 Comment

It's working now, thank you! I was just wondering, I already declared it in my props, but it didn't work still? <div>{props.date.toISOString()}</div>
0

I think in App.js when you loop through the expenses array, you want to return the ExpenseItem component, passing it the current expense item as a prop, like this:

{expenses.map((expense, index) => (
  <ExpenseItem item={expense} />
))}

And in ExpenseItem, since props is an object, you should change props.title to props.item.title etc.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.