Site icon C1CTech

ReactJS|Application folder structure

<h3><strong><span style&equals;"color&colon; &num;000080&semi;">React Folder Structure<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>In this article we will talk about the folder structure of react application&period;If you want to know what is react and how to create react application go to my previous article <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;react-introduction-and-creating-react-app&sol;">React Introduction and Creating react app<&sol;a><&sol;strong>&period;<&sol;span><&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"BYP60h975ew" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;BYP60h975ew" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;BYP60h975ew&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;BYP60h975ew&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<p>When we run the command in our terminal to create react application the following initial <strong>project structure<&sol;strong> get generated inside our project directory as shown below&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-910" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;ignorefile-1&period;png" alt&equals;"ignorefile" width&equals;"418" height&equals;"476" &sol;><&sol;p>&NewLine;<p>Now one by one we will talk about each file and directory our react project contains &colon;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>&period;gitignore<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>This is the standard file used by Git to determine which files and directories to ignore&comma; before you make a commit&period;<&sol;p>&NewLine;<p>A <strong><span style&equals;"color&colon; &num;0000ff&semi;"><em>&period;gitignore<&sol;em><&sol;span><&sol;strong> file should be committed into your repository&comma; in order to share the ignore rules with any other users that clone the repository&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-911" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;ignore2&period;png" alt&equals;"ignore2" width&equals;"694" height&equals;"662" &sol;><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>package&period;json<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;"alignnone size-full wp-image-919" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;packjs&period;png" alt&equals;"packjs" width&equals;"492" height&equals;"695" &sol;><&sol;p>&NewLine;<p>This file defines all the settings for the React project&period;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;000080&semi;">name<&sol;span><&sol;strong> is the name of your app&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;000080&semi;">version<&sol;span><&sol;strong> is the current version&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;000080&semi;"><strong>&&num;8220&semi;private&&num;8221&semi;&colon; true<&sol;strong><&sol;span> is a failsafe setting to avoid accidentally publishing your app as a public package within the npm ecosystem&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;000080&semi;"><strong>dependencies<&sol;strong><&sol;span> contains all the required node modules and versions required for the application&period; In the screenshot above&comma; the react version specified is <span style&equals;"color&colon; &num;0000ff&semi;">&Hat;<strong>16&period;8&period;3<&sol;strong><&sol;span>&period; This means that npm will install the most recent major version matching 16&period;x&period;x&period;<&sol;li>&NewLine;<li> It contains bydefault three Third party packages&comma; which allow us to use <span style&equals;"color&colon; &num;0000ff&semi;"><strong>react<&sol;strong><&sol;span> &comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;">react-dom<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;">react-scripts<&sol;span><&sol;strong> in our react project&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>react<&sol;strong><&sol;span> &colon; This package is used to build the  UI of our application&period;<&sol;p>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>react-dom<&sol;strong><&sol;span> &colon; This package is used by react for working with DOM&period;<&sol;p>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>react-scripts<&sol;strong><&sol;span> &colon; It includes scripts and configuration for bable &comma; webpack used by create react app&period;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;000080&semi;">scripts<&sol;span><&sol;strong> specifies aliases that you can use to access some of the react-scripts commands in a more efficient manner&period; For example running <span style&equals;"color&colon; &num;0000ff&semi;"><strong>npm test<&sol;strong><&sol;span> in your command line will run <strong><span style&equals;"color&colon; &num;0000ff&semi;">react-scripts test<&sol;span><&sol;strong> behind the scenes&period;There are few scripts that you can use &colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><strong><span style&equals;"color&colon; &num;0000ff&semi;">start<&sol;span><&sol;strong> &colon; <span style&equals;"color&colon; var&lpar;--color-neutral-600&rpar;&semi;">Runs the app in the development mode&period;It<&sol;span> is run by the <strong><span style&equals;"color&colon; &num;008000&semi;"> &&num;8220&semi;npm start&&num;8221&semi;<&sol;span><&sol;strong> command&period;<&sol;p>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><strong><span style&equals;"color&colon; &num;0000ff&semi;">build<&sol;span><&sol;strong> &colon; <span style&equals;"color&colon; var&lpar;--color-neutral-600&rpar;&semi;">Builds the app for production to the &grave;<strong>build<&sol;strong>&grave; folder&period; <&sol;span><span style&equals;"color&colon; var&lpar;--color-neutral-600&rpar;&semi;">It correctly bundles React in production mode and optimizes the build for the best performance&period; It is run by the  <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;npm run build&&num;8221&semi;<&sol;span><&sol;strong> command&period;<&sol;span><&sol;p>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><strong><span style&equals;"color&colon; &num;0000ff&semi;">test<&sol;span><&sol;strong> &colon; <span style&equals;"color&colon; var&lpar;--color-neutral-600&rpar;&semi;">Launches the test runner in the interactive watch mode&period;It is run by the  <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;npm test&&num;8221&semi;<&sol;span> <&sol;strong>command&period;<&sol;span><&sol;p>&NewLine;<p style&equals;"padding-left&colon; 60px&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>eject<&sol;strong><&sol;span> &colon; This script is only used when you want to customize react-scripts and go on with your own&period;It<span style&equals;"color&colon; var&lpar;--color-neutral-600&rpar;&semi;"> is run by the  <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;npm run eject&&num;8221&semi;<&sol;span><&sol;strong> command&period;<&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>package-lock&period;json <&sol;strong><&sol;span><&sol;h4>&NewLine;<p>This file contains the exact dependency tree installed in <span style&equals;"color&colon; &num;0000ff&semi;"><strong>node&lowbar;modules&sol;<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>This file is automatically generated by <strong><span style&equals;"color&colon; &num;0000ff&semi;">package&period;json<&sol;span><&sol;strong>&period;It makes sure that your version numbers of your dependencies are locked in so if I gave this project to anybody else anywhere around the world we make sure that the versions are correct so that it works 100 percent of the time&period;<&sol;p>&NewLine;<p>It also contains a history of changes to <strong><span style&equals;"color&colon; &num;0000ff&semi;">package&period;json<&sol;span><&sol;strong>&comma; so you can quickly look back at dependency changes&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;003366&semi;"><strong>README&period;md<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>This file contains just the information that you remember&period; When you put it on <strong>Github<&sol;strong> it will display in the project directory&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>node&lowbar;modules<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>This directory contains dependencies and sub-dependencies of packages used by the current React app&comma; as specified by <span style&equals;"color&colon; &num;0000ff&semi;"><strong>package&period;json<&sol;strong><&sol;span>&period; If you take a look&comma; you may be surprised by how many there are&period;There is no need to share this folder on github that&&num;8217&semi;s why this folder is automatically added to the <strong>&period;<span style&equals;"color&colon; &num;0000ff&semi;">gitignore<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>Now when we clone project from github &comma;Inside the project directory  we have to run the command <span style&equals;"color&colon; &num;008000&semi;"><strong>npm install <&sol;strong><&sol;span>and it will create <span style&equals;"color&colon; &num;0000ff&semi;"><strong>node&lowbar;modules<&sol;strong><&sol;span> directory inside your project directory and inside <span style&equals;"color&colon; &num;000000&semi;">node&lowbar;modules<&sol;span> directory all the dependencies you have defined inside <span style&equals;"color&colon; &num;0000ff&semi;"><strong>package&period;json<&sol;strong><&sol;span> get installed automatically&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>public<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;"alignnone size-full wp-image-921" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;public2-1&period;png" alt&equals;"public2" width&equals;"272" height&equals;"145" &sol;><&sol;p>&NewLine;<p>This directory contains assets that will be served directly without additional processing by <strong><span style&equals;"color&colon; &num;0000ff&semi;">webpack<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>index&period;html<&sol;strong><&sol;span> &colon; provides the entry point for the web app&period;It consist of only one div element which we will grab in our <strong><span style&equals;"color&colon; &num;0000ff&semi;">src&sol;index&period;js<&sol;span><&sol;strong>&lpar; javascript entry point&rpar; by its id and inside which we will render our main App javascript component ie&comma; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>App&period;js<&sol;strong><&sol;span>&period; When we run our app &comma;what we see up on the browser is App&period;js component&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>favicon&period;ico &lpar;header icon&rpar;<&sol;strong><&sol;span> &colon; It contains icon that can you see in the tab of your browser&period;There are online tools available like <a href&equals;"https&colon;&sol;&sol;realfavicongenerator&period;net&sol;"><strong><span style&equals;"color&colon; &num;008000&semi;">favicon generator<&sol;span><&sol;strong><&sol;a> using which we can create our own <strong>favicon&period;ico<&sol;strong> by providing it icon image&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">manifest&period;json<&sol;span> &colon; <&sol;strong>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">manifest&period;json<&sol;span><&sol;strong> is a simple JSON file that tells the browser about your web application and how it should behave when &&num;8216&semi;installed&&num;8217&semi; on the user&&num;8217&semi;s mobile device or desktop&period;Here behave means if we talk about mobile devices then how it look like in mobile&&num;8217&semi;s home screen&comma; how the app is going to launch and how its going to look&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>src<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;"alignnone size-full wp-image-922" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;02&sol;public&period;png" alt&equals;"public" width&equals;"308" height&equals;"269" &sol;><&sol;p>&NewLine;<p>This is the heart of the React app&period; For faster rebuilds&comma; only files inside <strong><span style&equals;"color&colon; &num;0000ff&semi;">src<&sol;span><&sol;strong> are processed by Webpack&period; You need to <strong><span style&equals;"color&colon; &num;0000ff&semi;">put any JS and CSS files inside<&sol;span> src<&sol;strong>&comma; otherwise Webpack won’t see them&period;  Browsing this folder&comma; you see the main App JavaScript component &lpar;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>App&period;js<&sol;strong><&sol;span>&rpar;&comma; its associated styles &lpar;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>App&period;css<&sol;strong><&sol;span>&rpar;&comma; and test suite &lpar;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>App&period;test&period;js<&sol;strong><&sol;span>&rpar;&period; index&period;js and its styles &lpar;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>index&period;css<&sol;strong><&sol;span>&rpar; provide an entry into the App and also kicks off the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>serviceWorker&period;js<&sol;strong><&sol;span>&period; This service worker takes care of caching and updating files for the end-user&period; It allows for offline capability and faster page loads after the initial visit&period; More on this methodology is available here <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"http&colon;&sol;&sol;bit&period;ly&sol;CRA-PWA">http&colon;&sol;&sol;bit&period;ly&sol;CRA-PWA<&sol;a>&period;<&sol;span><&sol;strong><&sol;p>&NewLine;<p>As your React app grows&comma;You may also create subdirectories inside src&period; it is common to add a <strong><span style&equals;"color&colon; &num;0000ff&semi;">components<&sol;span>&sol;<&sol;strong> directory to organize components and component-related files and a <strong><span style&equals;"color&colon; &num;0000ff&semi;">views&sol;<&sol;span><&sol;strong> directory to organize React views and view-related files&period;<&sol;p>&NewLine;<p><strong>I hope now you have a clear understanding of your react application folder structure and also what exactly these  files and folders contains when you create react application&period;<&sol;strong>&NewLine;

Exit mobile version