简体   繁体   English

如何修复 React 中的“TypeError:moment is not a function”?

[英]How to fix 'TypeError: moment is not a function' in React?

I've tried downgrading to Moment.js version 2.24.0, but that did not fix my issue.我尝试降级到 Moment.js 版本 2.24.0,但这并没有解决我的问题。 Below is my code.下面是我的代码。 Is there something I'm doing wrong?有什么我做错了吗? The error is pointing to my formatDate function, but I can't seem to pinpoint the issue.错误指向我的 formatDate function,但我似乎无法确定问题所在。

import moment from 'moment';

class ViewMorePhotos extends Component {
    state = {
        date: moment(),
        photo: ""
    };
    
    formatDate = moment => {
        let year = moment().year();
        let month = moment().month() + 1;
        let day = moment().date();
        return `${year}-${month}-${day}`;
    };
    
    changeDate = dateFromInput => {
        this.setState({ date: dateFromInput });
        this.getPhoto(this.formatDate(dateFromInput));
    };

    getPhoto = date => {
        fetch( `api-url`)
            .then(response => response.json())
            .then(photoData => this.setState({ photo: photoData }));
    };
    
    render() {
        return (
            <React.Fragment>
                <NavBar />
                <DateInput changeDate={this.changeDate} date={this.state.date} />
                <Photo photo={this.state.photo} />
            </React.Fragment>
        );
    }
}

This is the error I receive:这是我收到的错误:

TypeError: moment is not a function
ViewMorePhotos/this.formatDate
src/components/ViewMorePhotos.js:17

  14 | };
  15 | 
  16 | formatDate = moment => {
> 17 |     let year = moment().year();
     | ^  18 |     let month = moment().month() + 1;
  19 |     let day = moment().date();
  20 |     return `${year}-${month}-${day}`;

ViewMorePhotos/this.changeDate
src/components/ViewMorePhotos.js:25

  22 | 
  23 | changeDate = dateFromInput => {
  24 |     this.setState({ date: dateFromInput });
> 25 |     this.getPhoto(this.formatDate(dateFromInput));
     | ^  26 | };
  27 | 
  28 | getPhoto = date => {

Change your function to将您的 function 更改为

formatDate = aDate => {
    let m = moment(aDate)
    let year = m.year();
    let month = m.month() + 1;
    let day = m.date();
    return `${year}-${month}-${day}`;
};

Not tested but should work.未经测试,但应该可以工作。

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

相关问题 TypeError:moment(...)。tz不是函数 - TypeError: moment(…).tz is not a function TypeError: moment().tz 不是函数 - TypeError: moment().tz is not a function 如何在React中修复“ TypeError:category.map不是函数”错误 - How to fix “TypeError: categories.map is not a function” error in React 如何在 React 中修复“TypeError: TeachingExp.map 不是函数” - How to fix "TypeError: teachingExp.map is not a function", in React 如何修复 TypeError:this.props.changeCategory 在 React 中不是 function? - How to fix TypeError: this.props.changeCategory is not a function in React? 如何在 React 中修复“TypeError: results.map is not a function” - How to fix "TypeError: results.map is not a function", in React React:如何解决&#39;Uncaught TypeError:this.state.data.map不是一个函数&#39; - React : How to fix ' Uncaught TypeError: this.state.data.map is not a function' 如何在 REACT 中修复 TypeError Object 而不是 function? - How do I fix the TypeError Object not a function in REACT? 如何修复 React Context 的对象不是函数 - TypeError - How to fix React Context's object is not a function - TypeError 如何修复 TypeError:this.state.Templatelist.map 不是反应中的函数? - How to fix TypeError: this.state.Templatelist.map is not a function in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM