The error is self-explanatory as it is. The error "Only one default export allowed per module" occurs on React when exporting more than one function as default. Here is an example of the code that would throw this error.
As you can see, there are two functions with "export default" keywords in front of the function definition.
You can export a function in two ways, "default" export and "named" export.
Default export
The functions written in the above code are of default export type. What it means is that when you import a javascript file, this function will be imported by default. Hence, there cannot be two default export functions in a single javascript file.
Importing a default exported function is as easy as importing the file itself. You can also import the function with any name you like. See the example below:
Named export
Functions that are exported without the default keyword. You can have any number of named export in a javascript file. For example:
To import these functions into another javascript file, we need to perform what we call a "named import". It will look as follows:
In "named imports", the importing function name should be exactly in its definition. Named imports are wrapped in curly braces to distinguish from default imports.
In real-world projects, it is a common practice to use "named exports" through out the project to make effecient use of the ediotrs auto import feature as well as to maintain consistency acrross the codebase.
Combined usage
You can import both types of export from a javascript file in a single line like in the example below:
Conclusion
The error "Only one default export allowed per module" occurred because of the existence of multiple default exports. The above examples gave an idea of how you can import "default export" and "named export" functions in a React project.
Thank you for reading, please visit my home page to see more articles which might interest you.