Promptless AI is here soon - Production-ready contextual code. Don't just take our word for it. Know more
Know More

React Code Formatting Made Easy: A Journey with Prettier

No items found.

Kesar Bhimani

August 9, 2023

Kesar Bhimani

August 9, 2023

When I first started with React, I was blown away by its power and flexibility. But as my projects grew in complexity, I found myself spending more time formatting my code than actually writing it. That’s when I discovered the opinionated React formatter called Prettier, a tool that has since become an indispensable part of my development workflow, and in this article, I will share my insights on how to format a React app with Prettier.

Understanding Prettier

Prettier Logo PNG Vector

Prettier is an opinionated code formatter that supports a variety of languages, including JavaScript, which is the backbone of any React app. The term "opinionated code formatter" means that Prettier has default options for how it formats code. These default options are designed to promote consistency and readability in your codebase.

One of the main advantages of using Prettier in your React app is that it helps to automatically format your code. This means that you don't have to spend time manually formatting your code, which can be a tedious task, especially in large projects. Instead, you can focus on writing code and let Prettier handle the formatting.

Another advantage of Prettier is its seamless integration with code editors like Visual Studio Code. Once you have installed the Prettier extension in Visual Studio Code, you can easily format your code with a simple command from the command palette or even configure it to format on save. This means every time you save your file, your code gets automatically formatted according to the rules defined in your configuration file.

How Prettier Works

Prettier works by parsing your code into an abstract syntax tree (AST), which is a tree representation of your code structure. It then traverses this tree and prints out the code in a formatted manner according to the rules defined in your configuration file.

One of the key features of Prettier is that it formats your code in a consistent manner, regardless of the original code style. This means that even if different developers in your team have different coding styles, Prettier will format the code in a consistent way, making it easier to read and understand.

Here's an example of how Prettier can format a piece of JavaScript code in a React app:

As you can see, Prettier has formatted the code to make it more readable. It has added a new line between the function declaration and the return statement, and it has also removed the unnecessary parentheses around the JSX.

Setting Up React Formatter in Visual Studio Code

To start using the React Formatter, you'll need to install it in your Visual Studio Code. Here's how:

  1. Open Visual Studio Code.
  2. Go to the Extensions view (Ctrl+Shift+X).
  3. In the search box, type 'Prettier - Code formatter'.
  4. Click 'Install'.

Once installed, you can configure it as your default formatter. Go to your settings (Ctrl+,), search for "Default Formatter", and select "Prettier - Code formatter". Now, every time you save a file, the formatter will automatically format your code.

Configuring React Formatter

To configure the React Formatter, you'll need to create a .prettierrc file in your project root.

Specifying Formatting Rules

In your .prettierrc file, you can specify the formatting rules for your project. Here's an example:

In this example, the React Formatter will use single quotes, add a trailing comma where possible, and wrap lines longer than 80 characters.

Ignoring Files or Directories

You can also create a .prettierignore file in the root directory of your project to specify files or directories that the formatter should ignore.

You can list multiple files and directories in your .prettierignore file, one per line. You can also use glob patterns to match multiple files or directories. For example, .log will match all log files, and */node_modules will match the node_modules directory in any subdirectory.

Using React Formatter

To format a file, open the Command Palette (Ctrl+Shift+P), type "Format Document", and hit Enter. The formatter will automatically format your code according to the rules specified in your .prettierrc file.

You can also enable "Format On Save" in your settings to automatically format your code every time you save a file.

Transforming React Development: Automate API Tasks with WiseGPT for Enhanced Productivity

In wrapping up, it's clear that Prettier is a game-changer for any React developer. It streamlines the coding process, ensuring consistent formatting and freeing up your time to focus on what truly matters - crafting logic and creating value. But what if we could take this a step further?

This is where WiseGPT comes into play. Developed by DhiWise, WiseGPT is a plugin that seamlessly integrates into your development workflow, taking the automation of tasks to the next level. It's not just about formatting; it's about generating code for APIs into your React project, mirroring your unique coding style, and auto-creating models and functions.

Think about it. What if you no longer had to manually write API requests or parse responses? What if managing errors for complex API endpoints could be handled automatically? How much more could you achieve if these time-consuming tasks were taken care of?

That's the power of WiseGPT. It's not just a plugin; it's a productivity booster that handles the complexities of API integration, leaving you to focus on what you do best - building amazing applications.

These are not just hypothetical scenarios. This is what WiseGPT brings to your development process. So, why not give it a try? Experience the difference that WiseGPT can make in your React development process. Happy coding!

Frequently asked questions

Frequently asked questions

No items found.