React is a very popular front end java-script library which is created and maintained by Facebook. It can be used to build responsive single page applications. There are several ways to initiate a react project. Either you can build from scratch or use a tool-chain recommended by the react team.
Building from scratch
To build a react project from scratch you have to initiate a node project by issuing the command 'npm init'. Then you can issue the command 'npm install react-router-dom' to install the dependencies related to react.
Then you have to add 2 files, index.html and index.js. Instead of index.js index.jsx also can be used. These files can be added to the root directory itself. When building an application from scratch there is no restrictions as to the directory structure. But it is always a good practice to maintain proper directory structure. When building from scratch you can also use tools like webpack or parcel for bundling.
Building using create-react-app
The react team recommends several tool chains for building a react application rather than building a react application from scratch. Among those tools there are ones like Next.js, Gatsby. In this article let us focus on Create React App which the team describes as "a comfortable environment for learning react".
When using this tool chain you have to issue the command 'npx create-react-app <app-name>' (assuming that node.js is already installed) and it will create a react application with react dependencies with a folder structure. This method is an officially supported way of creating react applications and gives build with no configurations. Obviously in a convenience perspective this is the easier way to create a react application. Once created, using 'npm start' you can start the application.
However you must note that index.js and index.html whcih are created must not be altered and js and css files should be only put in src directory for webpack to pickup them. This tool chain uses webpack , Babel and ESLint among other projects.
Which should you use?
For most, they will feel like using create react app will be more suitable for them as it is easy to setup and get running. Even for beginners, a react application can be set up easily that way. It manages all the dependencies and configurations for you and even updating is easier.I
If one needs more control over which dependencies and modules they use from the beginning they will benefit more from building the react application from scratch. However, if one needs to take control of configuration, when using create react app, they can "eject" from create react app and edit configuration files directly. Also, it will be helpful for react developers to know the process and tools involved in building a react application from scratch and for that at some point they should have created a react application from scratch.
So, when determining which method should one use when creating a react application, it concerns the aspects like the degree of configurations that we need to perform and tools that we need to use.
Comments
Post a Comment