Export a function from a JavaScript file

Tags: , ,



I am trying to make a component in React that is just a function

import React from "react";

function TableSortFunction(data, method, column) {
  const newList = [];
  for (let i; i <= data.length; i++) {
    newList.push(data[i].column);
  }
  console.log(newList), data, method, column;
  return newList;
}

export default TableSortFunction;

I am importing my function in another component like this

import { TableSortFunction } from "./TableSortFunction";

but I get an error saying:

Attempted import error: 'TableSortFunction' is not exported from './TableSortFunction'.

How do I export a js file that is just a function?

Answer

Since you export it as default you have to import it like so:

import TableSortFunction from "./TableSortFunction";

Edit: Another issue with your code is that the following is not syntactically correct.

console.log(newList), data, method, column;

Try this instead:

console.log(newList, data, method, column);


Source: stackoverflow