{"id":2716,"date":"2021-08-24T01:20:00","date_gmt":"2021-08-24T06:20:00","guid":{"rendered":"http:\/\/upmostlymulti.onpressidium.com\/?p=2716"},"modified":"2022-10-28T11:03:08","modified_gmt":"2022-10-28T16:03:08","slug":"react-import-component","status":"publish","type":"post","link":"http:\/\/upmostly.com\/tutorials\/react-import-component","title":{"rendered":"Importing Components in React From Other Files: Step-by-Step Tutorial"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"http:\/\/upmostly.com\/wp-content\/uploads\/Importing-Components-in-React.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"530\" src=\"http:\/\/upmostly.com\/wp-content\/uploads\/Importing-Components-in-React-1024x530.png\" alt=\"\" class=\"wp-image-2761\" srcset=\"https:\/\/upmostly.com\/wp-content\/uploads\/Importing-Components-in-React-1024x530.png 1024w, https:\/\/upmostly.com\/wp-content\/uploads\/Importing-Components-in-React-300x155.png 300w, https:\/\/upmostly.com\/wp-content\/uploads\/Importing-Components-in-React-768x397.png 768w, https:\/\/upmostly.com\/wp-content\/uploads\/Importing-Components-in-React.png 1160w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>React allows you to build very complex applications. But as your applications grow in size, your code can become messy and difficult to build on.<\/p>\n\n\n\n<p>Sometimes, we&#8217;d like to split different React components into logically separated files. This act of separating functionality is a common operation in almost any programming language, from Python to C++. <\/p>\n\n\n\n<p>In this article, we will cover how to split React components into different files, and enable them to reference each other. By the end of this article, you&#8217;ll know how to import <a href=\"http:\/\/upmostly.com\/tutorials\/react-functional-vs-class-components\">class components and functional components in React<\/a> as well as understand a key limitation. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Background: Multi-file JavaScript<\/h2>\n\n\n\n<p>Since React is a JavaScript library, much of the programming is done in the exact same way across the two. Imports are no exception. <\/p>\n\n\n\n<p>We&#8217;ll see a quick example of importing functions and classes from other files in JavaScript below:<\/p>\n\n\n\n<p>In a file named <strong><span class=\"has-inline-color has-vivid-red-color\">importFrom.js:<\/span><\/strong><\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">importFrom.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">function<\/span> <span class=\"token function\">printToScreen<\/span><span class=\"token punctuation\">(<\/span> message <span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n    console<span class=\"token punctuation\">.<\/span><span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span> message <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n\r\n<span class=\"token keyword\">export<\/span> <span class=\"token punctuation\">{<\/span> printToScreen <span class=\"token punctuation\">}<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>Then, in a file named <strong><span class=\"has-inline-color has-vivid-red-color\">import.js<\/span><\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">import.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> printToScreen <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">\".\/importFrom\"<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n<span class=\"token function\">printToScreen<\/span><span class=\"token punctuation\">(<\/span> <span class=\"token string\">\"Test Message\"<\/span> <span class=\"token punctuation\">)<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>When <strong><span class=\"has-inline-color has-vivid-red-color\">import.js<\/span><\/strong> is run, &#8220;Test Message&#8221; is logged to the console. Let&#8217;s walk through the different key parts of this example.<\/p>\n\n\n\n<p>In <strong><span class=\"has-inline-color has-vivid-red-color\">importFrom.js<\/span><\/strong>, we define the function <strong><em>printToScreen<\/em><\/strong> as you&#8217;d expect, with a very simple execution. Then, we use the keyword &#8220;export&#8221; followed by a set of curly braces &#8220;{}&#8221; with the function <strong><em>printToScreen<\/em><\/strong> within. <\/p>\n\n\n\n<p>This exposes the function <strong><em>printToScreen<\/em><\/strong> to the directory, so that other files can reference the function. Then, in <strong><span class=\"has-inline-color has-vivid-red-color\">import.js<\/span><\/strong>, we use the &#8220;import&#8221; keyword to reference the <strong><em>printToScreen<\/em><\/strong> function in <strong><span class=\"has-inline-color has-vivid-red-color\">importFrom.js<\/span><\/strong>. <\/p>\n\n\n\n<p>Notice how we use curly braces &#8220;{}&#8221; here as well, and put the function we&#8217;d like to import, <strong><em>printToScreen<\/em><\/strong>, within. This allows us to reference the function as if it was written directly in the file. <\/p>\n\n\n\n<p>Notice also how we use the relative path of the file, &#8220;.\/importFrom&#8221;. This means that we are importing from the same directory. <\/p>\n\n\n\n<p>We then use the function we imported to print &#8220;Test Message&#8221; to the console. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Functional Component Imports in React<\/h2>\n\n\n\n<p>In React, importing is the same as in vanilla JavaScript. Let&#8217;s do a similar example to the one above, but instead of printing &#8220;Test Message&#8221; to the console, we&#8217;ll render it to the screen:<\/p>\n\n\n\n<p>In the file <strong><span class=\"has-inline-color has-vivid-red-color\">App.js<\/span><\/strong> automatically generated by the <strong>create-react-app<\/strong> command: <\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">App.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/ImportElement'<\/span><span class=\"token punctuation\">;<\/span>\n\n<span class=\"token keyword\">function<\/span> <span class=\"token function\">App<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>ImportComponent<\/span> <span class=\"token punctuation\">\/><\/span><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token punctuation\">}<\/span>\n\n<span class=\"token keyword\">export<\/span> <span class=\"token keyword\">default<\/span> App<span class=\"token punctuation\">;<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>Then, in a file called <strong><span class=\"has-inline-color has-vivid-red-color\">ImportElement.js <\/span><\/strong>in the same directory:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">ImportElement.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">function<\/span> <span class=\"token function\">ImportComponent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\r\n        <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>div<\/span><span class=\"token punctuation\">><\/span><\/span><span class=\"token plain-text\">\r\n            Test Message\r\n        <\/span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;\/<\/span>div<\/span><span class=\"token punctuation\">><\/span><\/span>\r\n    <span class=\"token punctuation\">)<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n\r\n<span class=\"token keyword\">export<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>We can see many similarities when importing the functional component <strong><em>ImportComponent.<\/em><\/strong> The <em>import <\/em>and <em>export<\/em> keywords are used in the exact same way.<\/p>\n\n\n\n<p>Since we are using JSX, we can call <strong><em>ImportComponent<\/em><\/strong> just by typing &#8220;&lt;ImportComponent \/&gt;&#8221;, rather than needing to call the function itself explicitly. <\/p>\n\n\n\n<p>Of course, there is a difference in <strong><span class=\"has-inline-color has-vivid-red-color\">ImportElement.js<\/span><\/strong> as well, as the function must return a JSX component instead of just printing to the console. <\/p>\n\n\n\n<p>Otherwise, we see that the React and JavaScript imports are identical. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Class Component Imports in React<\/h2>\n\n\n\n<p>What about class component imports? <\/p>\n\n\n\n<p>We&#8217;ll first see an example in vanilla JavaScript:<\/p>\n\n\n\n<p>As before, we start with a file called <strong><span class=\"has-inline-color has-vivid-red-color\">importFrom.js<\/span><\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">importFrom.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">printToScreen<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token function\">constructor<\/span><span class=\"token punctuation\">(<\/span> message <span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n        console<span class=\"token punctuation\">.<\/span><span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span> message <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n\r\n<span class=\"token keyword\">export<\/span> <span class=\"token punctuation\">{<\/span> printToScreen <span class=\"token punctuation\">}<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>And a file called<strong><span class=\"has-inline-color has-vivid-red-color\"> import.js:<\/span><\/strong><\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">import.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> printToScreen <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">\".\/importFrom\"<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n<span class=\"token keyword\">var<\/span> printToScreenElem <span class=\"token operator\">=<\/span> <span class=\"token function\">printToScreen<\/span><span class=\"token punctuation\">(<\/span> <span class=\"token string\">\"Test Message\"<\/span> <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>As before, when <strong><span class=\"has-inline-color has-vivid-red-color\">import.js<\/span><\/strong> is run, &#8220;Test Message&#8221; is logged to the console. <\/p>\n\n\n\n<p>And as before, the import process is identical in React:<\/p>\n\n\n\n<p>In <strong><span class=\"has-inline-color has-vivid-red-color\">App.js:<\/span><\/strong><\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">App.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/ImportElement'<\/span><span class=\"token punctuation\">;<\/span>\n\n<span class=\"token keyword\">function<\/span> <span class=\"token function\">App<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>ImportComponent<\/span> <span class=\"token punctuation\">\/><\/span><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token punctuation\">}<\/span>\n\n<span class=\"token keyword\">export<\/span> <span class=\"token keyword\">default<\/span> App<span class=\"token punctuation\">;<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>In <strong><span class=\"has-inline-color has-vivid-red-color\">ImportElement.js<\/span><\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">ImportElement.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> React <span class=\"token keyword\">from<\/span> <span class=\"token string\">\"react\"<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ImportComponent<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">React<span class=\"token punctuation\">.<\/span>Component<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token function\">render<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\r\n            <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>div<\/span><span class=\"token punctuation\">><\/span><\/span><span class=\"token plain-text\">\r\n                Test Message\r\n            <\/span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;\/<\/span>div<\/span><span class=\"token punctuation\">><\/span><\/span>\r\n        <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span> \r\n<span class=\"token punctuation\">}<\/span>\r\n\r\n<span class=\"token keyword\">export<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>Once again, since we are using React and JSX, the creation and calling of the class <strong>ImportComponent<\/strong> is different. <\/p>\n\n\n\n<p>However, the export and import process is identical as in vanilla JavaScript. <\/p>\n\n\n\n<p>Here&#8217;s what the output looks like:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/upmostly.com\/wp-content\/uploads\/Capture-10.png\" alt=\"\" class=\"wp-image-2726\" width=\"327\" height=\"158\" srcset=\"https:\/\/upmostly.com\/wp-content\/uploads\/Capture-10.png 350w, https:\/\/upmostly.com\/wp-content\/uploads\/Capture-10-300x145.png 300w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Importing from Another Directory in React<\/h2>\n\n\n\n<p>So far, we&#8217;ve been exporting and importing from the same directory. What if we have subdirectories that we want to pull React component definitions from? <\/p>\n\n\n\n<p>All we have to do is change the path of the file we are importing from. This element is located in a subdirectory called &#8220;importSubdirectory&#8221;, and the file is called <strong><span class=\"has-inline-color has-vivid-red-color\">ImportElement.js<\/span><\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">ImportElement.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> React <span class=\"token keyword\">from<\/span> <span class=\"token string\">\"react\"<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ImportComponent<\/span> <span class=\"token keyword\">extends<\/span> <span class=\"token class-name\">React<span class=\"token punctuation\">.<\/span>Component<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token function\">render<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\r\n            <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>div<\/span><span class=\"token punctuation\">><\/span><\/span><span class=\"token plain-text\">\r\n                Test Message\r\n            <\/span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;\/<\/span>div<\/span><span class=\"token punctuation\">><\/span><\/span>\r\n        <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span> \r\n<span class=\"token punctuation\">}<\/span>\r\n\r\n<span class=\"token keyword\">export<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>We import this class component from <strong><span class=\"has-inline-color has-vivid-red-color\">App.js<\/span><\/strong>, which is in the default directory:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">App.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'.\/importSubdirectory\/ImportElement'<\/span><span class=\"token punctuation\">;<\/span>\n\n<span class=\"token keyword\">function<\/span> <span class=\"token function\">App<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>ImportComponent<\/span> <span class=\"token punctuation\">\/><\/span><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token punctuation\">}<\/span>\n\n<span class=\"token keyword\">export<\/span> <span class=\"token keyword\">default<\/span> App<span class=\"token punctuation\">;<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>As you can see, all we did was change the path we import from in <strong><span class=\"has-inline-color has-vivid-red-color\">App.js<\/span><\/strong>! The output is the same as before:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"169\" src=\"http:\/\/upmostly.com\/wp-content\/uploads\/Capture-11.png\" alt=\"\" class=\"wp-image-2727\" srcset=\"https:\/\/upmostly.com\/wp-content\/uploads\/Capture-11.png 350w, https:\/\/upmostly.com\/wp-content\/uploads\/Capture-11-300x145.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/figure>\n<\/div>\n\n\n<p>What if <strong><span class=\"has-inline-color has-vivid-red-color\">App.js <\/span><\/strong>was located in &#8220;importSubdirectory&#8221; instead, and <strong><span class=\"has-inline-color has-vivid-red-color\">ImportElement.js<\/span><\/strong> in the default directory? All we&#8217;d have to do is change the path in <strong><span class=\"has-inline-color has-vivid-red-color\">App.js<\/span><\/strong> once again:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">App.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'..\/ImportElement'<\/span><span class=\"token punctuation\">;<\/span>\n\n<span class=\"token keyword\">function<\/span> <span class=\"token function\">App<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>ImportComponent<\/span> <span class=\"token punctuation\">\/><\/span><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token punctuation\">}<\/span>\n\n<span class=\"token keyword\">export<\/span> <span class=\"token keyword\">default<\/span> App<span class=\"token punctuation\">;<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>In the example above, we changed the path to &#8220;..\/ImportElement&#8221; instead. <\/p>\n\n\n\n<p>If you&#8217;re unfamiliar with the &#8216;..&#8217; before the first slash, all it does is navigate one directory upward. So rather than importing from the directory &#8220;\/importSubdirectory\/&#8221;, we import from the root directory, &#8220;\/&#8221;, where <strong><span class=\"has-inline-color has-vivid-red-color\">ImportElement.js<\/span><\/strong> is located. <\/p>\n\n\n\n<p>The output is exactly the same:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"169\" src=\"http:\/\/upmostly.com\/wp-content\/uploads\/Capture-12.png\" alt=\"\" class=\"wp-image-2728\" srcset=\"https:\/\/upmostly.com\/wp-content\/uploads\/Capture-12.png 350w, https:\/\/upmostly.com\/wp-content\/uploads\/Capture-12-300x145.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Import Limitations in React<\/h2>\n\n\n\n<p>There is a key import limitation in React which we haven&#8217;t discussed thus far: you cannot import from files outside the src directory. The src directory is created by &#8216;create-react-app&#8217;, and is meant to encompass all <a href=\"http:\/\/upmostly.com\/tutorials\/react-dropzone-file-uploads-react\">React files<\/a> in your application.<\/p>\n\n\n\n<p>With the file structure below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"344\" height=\"274\" src=\"http:\/\/upmostly.com\/wp-content\/uploads\/Capture-13.png\" alt=\"\" class=\"wp-image-2730\" srcset=\"https:\/\/upmostly.com\/wp-content\/uploads\/Capture-13.png 344w, https:\/\/upmostly.com\/wp-content\/uploads\/Capture-13-300x239.png 300w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/figure>\n\n\n\n<p>We cannot import from the file <strong><span class=\"has-inline-color has-vivid-red-color\">importError.js<\/span><\/strong> from within the &#8216;src&#8217; directory. If we try to do so in <strong><span class=\"has-inline-color has-vivid-red-color\">App.js<\/span><\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-cgb-block-codeplus\"><div class=\"upmostly-block better-code-block\"><div class=\"dark\"><div class=\"code-editor-header\">importError.js<\/div><pre class=\"language-jsx\"><code class=\"language-jsx\"><div><span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> ImportComponent <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">'..\/..\/importError'<\/span><span class=\"token punctuation\">;<\/span>\n\n<span class=\"token keyword\">function<\/span> <span class=\"token function\">App<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\n  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">(<\/span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;<\/span>ImportComponent<\/span> <span class=\"token punctuation\">\/><\/span><\/span>\n  <span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\n<span class=\"token punctuation\">}<\/span>\n\n<span class=\"token keyword\">export<\/span> <span class=\"token keyword\">default<\/span> App<span class=\"token punctuation\">;<\/span><\/div><\/code><\/pre><\/div><\/div><\/div>\n\n\n\n<p>We&#8217;ll encounter the following error in the console: &#8220;Module not found: You attempted to import ..\/..\/importError which falls outside of the project src\/ directory. Relative imports outside of src\/ are not supported.&#8221;<\/p>\n\n\n\n<p>This means that if you have any existing JavaScript code you&#8217;d like to integrate with React, you have to place the entire source code within the &#8216;src&#8217; directory in your React app, and work from there. <\/p>\n\n\n\n<p>So long as you keep all your files within the &#8216;src&#8217; directory, React imports are powerful and straightforward, working exactly like their vanilla JavaScript counterparts. <\/p>\n\n\n\n<p>I hope you enjoyed learning how to import components in React! If you have any questions or comments, please leave them below.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>React allows you to build very complex applications. But as your applications grow in size, your code can become messy and difficult to build on. Sometimes, we&#8217;d like to split different React components into logically separated files. This act of separating functionality is a common operation in almost any programming language, from Python to C++. [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":2761,"comment_status":"open","ping_status":"open","sticky":false,"template":"post-tutorial-new.php","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[33,38],"class_list":["post-2716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-beginner-react-tutorials","tag-react-components"],"acf":[],"_links":{"self":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/2716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/comments?post=2716"}],"version-history":[{"count":5,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/2716\/revisions"}],"predecessor-version":[{"id":13062,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/posts\/2716\/revisions\/13062"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media\/2761"}],"wp:attachment":[{"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/media?parent=2716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/categories?post=2716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upmostly.com\/wp-json\/wp\/v2\/tags?post=2716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}