× logo Home HTML CSS Javascript React-App Angular.js logo
logo

REACT FUNCTION COMPONENT: EXPORT AND IMPORT

Eventually you will separate components into their own files. Since React Components are functions (or classes), you can use the standard import and export statements provided by JavaScript. For instance, you can define and export a component in one file:.

export

And import it in another file:

import

Note: If a Function Component is not defined, console log your exports and imports to get a better understanding of where you made a mistake. Maybe you used a named export and expected it to be a default export.
If you don't care about the component name by defining the variable, you can keep it as Anonymous Function Component when using a default export on the Function Component: reactfcAnonymous
However, when doing it this way, React Dev Tools cannot identify the component because it has no display name. You may see an Unknown Component in your browser's developer tools.

logo