Key Concepts
- ESModule
import
/export
syntaximport ReactDOM from 'react-dom/client'
export default App
- Top-level
App
component
# Check your npm version
npm -v
# npm 6.x
npm create vite@latest my-react-app --template react
# npm 7+, extra double-dash is needed:
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm i
npm run dev
New File Extensions!
.jsx
files let us use JSX without importing React- Node Files -
.cjs
files are "CommonJS" files. These are files that are meant to be run in the Node environment and use the CommonJSrequire()
/module.exports
syntax. - Browser Files - All
.js
/.jsx
files are counted as "modules" that are meant to be run in the browser, meaning we can use ESModulesimport
/export
syntax.
Key Folders / Files
package.json
- See scripts, dependencies, and "type".eslintrc.cjs
file provides linting rulespublic/
folder - Contains static assets (pictures, text files, etc...). See the docsindex.html
serves as the entry point. Loadssrc/main.js
src/
foldermain.jsx
renders the top-levelApp
component. We can removeStrictMode
App.jsx
the top-level component
See the "scripts"
section of the package.json
file.
npm run dev
npm run build
npm run preview
We can further organize this code. I like to create the following structure:
vite-project/
└── src/
│ ├── components/
│ │ ├── App.js
│ │ ├── OtherComponent1.js
│ │ └── OtherComponent2.js
│ ├── contexts/
│ │ ├── MyContext.js
│ │ └── MyContextProvider.js
│ ├── styles/
│ │ ├── App.css
│ │ ├── OtherComponent1.css
│ │ └── OtherComponent2.css
└── index.html