search

Home  >  Q&A  >  body text

Compilation error: Arrow functions must be exported as module default before assigning them to variables

<p>The following error occurred while compiling: </p> <p>A warning occurred during compilation. </p> <p>src/Task.js</p> <pre class="brush:php;toolbar:false;">Line 4, first character: assign the arrow function to a variable and then export it as the module default value import/no-anonymous-default-export</pre> ; <p>src/TaskList.js</p> <pre class="brush:php;toolbar:false;">Line 4, first character: assign the arrow function to a variable and then export it as the module default value import/no-anonymous-default-export</pre> ; <p>The following is my JS file:</p> <p>Task.js</p> <pre class="brush:php;toolbar:false;">import React from 'react'; import TaskList from './TaskList'; export default ({Task}) => { return ( <p> {TaskList.description} </p> ); }</pre> <p>TaskList.js</p> <pre class="brush:php;toolbar:false;">import React from 'react'; import Task from './Task.js' export default ({ tasks }) => { return ( <ul className="list-group"> {tasks.map(task => ( <li key={task.id} className ="list-group-item"> <Task task={task} /> </li> ))} </ul> ); }</pre> <p>I just started learning React and JavaScript. </p>
P粉760675452P粉760675452459 days ago526

reply all(1)I'll reply

  • P粉420868294

    P粉4208682942023-08-31 11:11:27

    For arrow functions, since they are anonymous, you need to assign it to a variable and then export it. Based on your code, this example should work (but don't forget to fill in the logic in the tasks.map function)

    import React from 'react';
    import TaskList from './TaskList';
    
    const Tasks = ({ tasks }) => {
      return tasks.map(task => ( ... ));
    } 
    
    export default Tasks;
    

    This is caused by the import/no-anonymous-default-export rule, which prevents a module's default export from being unnamed.

    Since this is just a lint warning and not a syntax error, your existing code will work if you disable the rule (but I recommend not to do this!).

    This rule is useful because ensuring that default exports are named helps improve the searchability of the code base by encouraging reuse of the same identifier at declaration locations and import locations.

    reply
    0
  • Cancelreply