最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Object(...) is not a function with React Table and moment.js - Stack Overflow

programmeradmin2浏览0评论

I am using React Table module, and I'm trying to make use of moment to display a better date format from the created_at field of the data passed to ReactTable component.

let columns = [{
  id: "createdAt",
  Header: "Created",
  accessor: a => <Fragment>{moment(a.created_at).format("MM DD YYYY")}</Fragment>
}];

But for some reason, it's whining with the error

TypeError: Object(...) is not a function

pointing to this line.

If I simply do a.created_at it will display it normally. The module is imported for sure.

I am using React Table module, and I'm trying to make use of moment to display a better date format from the created_at field of the data passed to ReactTable component.

let columns = [{
  id: "createdAt",
  Header: "Created",
  accessor: a => <Fragment>{moment(a.created_at).format("MM DD YYYY")}</Fragment>
}];

But for some reason, it's whining with the error

TypeError: Object(...) is not a function

pointing to this line.

If I simply do a.created_at it will display it normally. The module is imported for sure.

Share Improve this question asked Sep 26, 2018 at 7:06 user4383363user4383363 5
  • post all your code...are you sure a.created_at is not already a moment object? – Daniel Lizik Commented Sep 26, 2018 at 7:12
  • Works fine for me: codesandbox.io/s/p73pnxjwpj – user5734311 Commented Sep 26, 2018 at 7:21
  • @DanielLizik, yes, it is stored as datetime in the database. – user4383363 Commented Sep 26, 2018 at 7:42
  • @ChrisG, alone it works, but when specified in the react-table module the error happens. There must be an issue with react-table – user4383363 Commented Sep 26, 2018 at 7:43
  • @gamofe I updated the code example with <ReactTable>. – user5734311 Commented Sep 26, 2018 at 7:47
Add a comment  | 

2 Answers 2

Reset to default 27

I had this error when I am importing moment the wrong way

import {moment} from "moment"; // with errors 'Object(...) is not a function'
import moment from "moment"; //no more errors

It's may due to wrong import statement

import moment from 'moment';

don't write

import * as moment from 'moment';

or

import {moment} from 'moment';
发布评论

评论列表(0)

  1. 暂无评论