GithubHelp home page GithubHelp logo

vite-preset-react's Introduction

vite-preset-react

An all in one preset for writing React apps with the vite bundler.

Features:

  • Sets up Hot Module Replacement via @vitejs/plugin-react-refresh
  • Automatically injects React into your components, so you don't have to import React from 'react' in every file.
  • Remove devtools in production if needed

Installation

First install the preset package from npm:

npm install --save-dev vite-preset-react
# or
yarn add -D vite-preset-react

Enhance your vite config with the React preset plugin in your vite.config.ts or vite.config.js:

import { defineConfig } from "vite";
import react from "vite-preset-react";

export default defineConfig({
  plugins: [react()],
});

Options

Options can be passed to our preset plugin via the first argument:

export default defineConfig({
  plugins: [react({ removeDevtoolsInProd: true, injectReact: true })],
});

Available options

Option Type Default Description
removeDevtoolsInProd boolean false Removes React Devtools in production build
injectReact boolean true Injects import React from 'react' in every JS file to avoid importing it in every file manually

Using in official starter templates

If you are using the official react or react-ts template, and wanna switch to this one, follow this:

  1. Remove @vitejs/plugin-react-refresh from package.json.

  2. Install this preset:

npm install -D vite-preset-react

Or if you're a Yarn person

yarn add -D vite-preset-react
  1. If you're using react-ts template, open tsconfig.json, and change jsx field to preserve.

There!! You're all set!

Errors

Identifier 'React' has already been declared

This error occurs when manually import React in a file, with the config option injectReact set to true.

Solution would be to remove the import if it is the only file in the whole codebase importing React. Or you can set injectReact option to false. This will suppress this error and allow you to import React in every file.

Uncaught ReferenceError: React is not defined

This error arises when you have the option injectReact set to false, but you're not importing React in some of your files. Solution would be to set this option to true; Or put an import React from 'react' statement in every file containing JSX code.

Not importing React doesn't allow JSX in TSX files

If you're getting red squiggles under your JSX, follow this:

  1. Go to tsconfig.json
  2. Set the jsx option to preserve.

There, that should fix it.

License

MIT, see the license file.

vite-preset-react's People

Contributors

puruvj avatar sqreder avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.