Search code examples
reactjstypeerrordate-fns

Having trouble importing a function from date-fns in one component but not another?


I am trying to import this function into one of my components from the date-fns library like so:

import { eachMonthOfInterval } from "date-fns";

and I am testing it like this within the component:

  let result = eachMonthOfInterval({
      start: props.state.startDate ? new Date(props.state.startDate) : '', 
      end: props.state.endDate ? new Date(props.state.endDate) : ''
    })

  console.log(result)

Unfortunately, I keep getting a TypeError: Object(...) is not a function

What is odd is I can use format() from date-fns in another component just fine when importing like this:

import {format} from "date-fns";

So... is this a bug or am I doing something glaringly wrong here?

EDIT: Here is a link to the docs https://date-fns.org/v2.14.0/docs/eachMonthOfInterval


Solution

  • can you try this . first , in your terminal enter this(be sure terminal in project folder) :
    npm install date-fns --save

        import React, { Component } from 'react'
    import { eachMonthOfInterval } from 'date-fns'
    
    class TimeInterval extends Component {
       constructor(props) {
          super(props)
    
       }
    
       render() {
          var result = eachMonthOfInterval({
             start: new Date(2014, 1, 6),
             end: new Date(2014, 7, 10)
           })
           console.log(result);
          return (
             <div>
                <h1 id='title'>Interval</h1>
                <table id='students'>
                   <tbody>
    
                   </tbody>
                </table>
             </div>
          )
       }
    }
    
    export default TimeInterval;
    

    enter image description here