Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was trying to clean up this react component by extracting fillCalendar() from being a method of the component into it's own js file and importing it instead. Originally this.state.datesArray was set in a componentWillMount() lifecycle method. Now I'm trying to directly initialize it inside the constructor because this is what the react docs recommends. Doing it this way now throws a "TypeError: Object(...) is not a function" error and I don't know why. Here is what Calendar.js use to look like see here.

Calendar.js

import React, { Component } from 'react';
import { fillCalendar } from '../calendar.tools'

class Calendar extends Component {
  constructor(props) {
    super(props)
    this.state = {
      datesArray: fillCalendar(7, 2018),
      date: new Date(),
      monthIsOffset: false,
      monthOffset: new Date().getMonth(),
      yearOffset: new Date().getFullYear()
    }
  }
  render() {
    return (
      ...
    )
  }
}

calendar.tools.js

let fillCalendar = (month, year) => {
  let datesArray = []
  let monthStart = new Date(year,month,1).getDay()
  let yearType = false;
  let filledNodes = 0;
  // Check for leap year
  (year%4 === 0) ? 
    (year%100 === 0) ?
      (year%400) ? yearType = true : yearType = false : 
    yearType = true : 
  yearType = false
  const monthArrays = yearType ? [31,29,31,30,31,30,31,31,30,31,30,31] : [31,28,31,30,31,30,31,31,30,31,30,31]
  if (month === 0) { month = 12; }
  let leadDayStart = monthArrays[month-1] - monthStart + 1
  // Loop out lead date numbers
  for (let i = 0; i < monthStart; i++) {
    datesArray.push({date: leadDayStart, type: "leadDate", id: "leadDate" + i})
    leadDayStart++
    filledNodes++
  }
  if (month === 12) { month = 0; }
  // Loop out month's date numbers
  for (let i = 0; i < monthArrays[month]; i++) {
    datesArray.push({date: i + 1, type: "monthDate", id: "monthDate" + i})
    filledNodes++
  }
  // fill the empty remaining cells in the calendar
  let remainingNodes = 42 - filledNodes;
  for (let i = 0; i < remainingNodes; i++) {
    datesArray.push({date: i + 1, type: "postDate", id: "postDate" + i})
  }
  return datesArray
}
like image 291
Ryan Sam Avatar asked Aug 24 '18 04:08

Ryan Sam


People also ask

Is not a function is an instance of Object react?

The React. js "Uncaught TypeError: X is not a function" occurs when we try to call a value that is not a function as a function, e.g. calling the props object instead of a function. To solve the error, console. log the value you are calling and make sure it is a function.

Is not a function TypeError is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

How do you create formData object in react JS?

onSubmit = fields => { const { title, body, image } = fields; var formData = new FormData(); formData. append('title', title); formData. append('body', body); formData. append('image', image); console.


2 Answers

It looks fine, you just have to export your function that's it.

use

export let fillCalendar = (month, year) => { 

instead of

let fillCalendar = (month, year) => { 
like image 75
Amruth Avatar answered Sep 21 '22 07:09

Amruth


Just putting it out there....

I saw this error because I imported a function from the wrong module!

But I'm sure you'd never do that ;-)

(obviously a bit unlucky in that the function I imported had a common name - useParams in my case)

like image 20
ErichBSchulz Avatar answered Sep 19 '22 07:09

ErichBSchulz