简体   繁体   English

为什么 React 中的 JavaScript 方法未定义?

[英]Why JavaScript methods in React are undefined?

In this section, when I add the JavaScript method to my file, I get this error.在本节中,当我将 JavaScript 方法添加到我的文件时,出现此错误。

import './ExpenseItem.css';

function ExpenseItem(props) {
  const month = props.date.toLocaleString('en-US', { month: 'long' });
  const day = props.date.toLocaleString('en-US', { day: '2-digit' });
  const year = props.date.getFullYear();

  return (
    <div className='expense-item'>
      <div>
        <div>{month}</div>
        <div>{year}</div>
        <div>{day}</div>
      </div>
      <div className='expense-item__description'>
        <h2>{props.title}</h2>
        <div className='expense-item__price'>${props.amount}</div>
      </div>
    </div>
  );
}

export default ExpenseItem;

在此处输入图像描述

Native JavaScript method are available in React but it appears that you aren't passing date as a prop to ExpenseItem .本机 JavaScript 方法在 React 中可用,但您似乎没有将date作为道具传递给ExpenseItem You should have something like this in the parent component of ExpenseItem :你应该在ExpenseItem的父组件中有这样的东西:

<ExpenseItem date={yourDateVariable}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM