Compilation error: Arrow functions must be exported as module default before assigning them to variables
P粉760675452
2023-08-30 13:05:14
<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>
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)
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.