Often serve the front-end React app from the same host and port as their backend implementation.
Exemple:
/ - static server returns index.html with React app
/todos - static server returns index.html with React app
/api/todos - server handles any /api/* requests using the backend implementation
For that, it is only necessary to configure the file: src/setupProxy.js
example:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
// ...
};
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:5000',
changeOrigin: true,
})
);
};
npm run storybook
yarn storybook
Storybook is a development environment for React UI components. It allows you to browse a component library, view the different states of each component, and interactively develop and test components.
npm start
yarn start
[https://localhost:3000]
npm run build
yarn build
npm run build-storybook
yarn build-storybook
npm test
yarn test
This is your source code tree:
src
|-- assets
|-- components
|-- config // all aplications configs, external services and interceptores
|-- hooks
|-- mocks
|-- models
|-- pages
|-- routes
|-- services // all query and functions request
|-- utils
|-- Bootstrap.tsx
|-- App.tsx
|-- index.tsx
|-- react-app-env.d.ts
|-- reportWebVitals.ts
|-- service-worker.ts
|-- serviceWorkerRegistration.ts
|-- setupProxy.js
|-- setupTests.ts
...
This is your component structure:
...
components
|-- YourComponent
|-- index.tsx
|-- styles.ts
|-- YourComponent.spec.ts
|-- YourComponent.stories.ts
...
or
...
components
|-- YourComponent
|-- index.tsx
|-- styles.ts
|-- interfaces.ts
|-- YourComponent.ts
|-- YourComponent.spec.ts
|-- YourComponent.stories.ts
...
or
...
components
|-- YourContainerComponents
|-- Component1
|-- Component2
|-- Component3
|-- index.ts
...
This is your page structure:
...
Pages
|-- YourPage
|-- index.tsx
|-- styles.ts
|-- interface.ts
|-- YourPage.spec.ts
|-- YourPage.stories.ts
...
./assets
Here will be all your project assets as images, icons...
./components
Components are presentational only elements, grouping UI items
./configs
Here will be all configurations for external and internal services
./hooks
Services are responsible to handle the connection with all external elements, like APIs and global functions
./mocks
All mock data for your unit testes, examples and any mocks
./models
global interfaces and models that your project needs
./pages
Pages are mapped in routes and have all the containers needed to implement a functionality
./routes
Routes contains the react-router-dom
implementation to map the project's routes to the respective pages
./services
Here will be all query and request funcions
./styles
Here will be all global and css(relatives)
./utils
Directory to keep all utils functions to share all over the project
Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_.
WARNING: Do not store any secrets (such as private API keys) in your React app! Environment variables are embedded into the build, meaning anyone can view them by inspecting your app's files. Create .env.test.local and .env.local before run project
Adding Development Environment Variables In
To define permanent environment variables, create a file called .env in the root of your project:
``REACT_APP_NOT_SECRET_CODE=abcdef```
What other files can be used
.env
: Default..env.local
: Local overrides. This file is loaded for all environments except test..env.development
, , : Environment-specific settings..env.test.env.production.env.development.local
, , : Local overrides of environment-specific settings..env.test.local.env.production.local
Files on the left have more priority than files on the right:
npm start
: , , ,.env.development.local
.env.local
.env.development
.env
npm run build
: , , ,.env.production.local
.env.local
.env.production
.env
npm test
: , , (note is missing).env.test.local
.env.test.env
.env.local
Example
REACT_APP_VERSION=$npm_package_version
# also works:
# REACT_APP_VERSION=${npm_package_version}
DOMAIN=www.example.com
REACT_APP_FOO=$DOMAIN/foo
REACT_APP_BAR=$DOMAIN/bar
Analyzer your build bundle
npm run analyze
yarn analyze
Find and fix Javascrit and TypeScript problems according to pre-defined rules
npm run analyze
yarn analyze
Find and fix code formatted
npm run prettier
yarn prettier
The project is already configured with:
- React
- Typescript
- Jest
- Babel-root-import
- unit testing example
- Eslint
- Prettier
- Styled
- Styled Theming
- Husky
- Storybook
- PWA
- Integration back-end proxy (http-proxy)
This project was bootstrapped with Create React App.
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.
See the section about running tests for more information.
Builds the app for production to the build
folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
Your app is ready to be deployed!
See the section about deployment for more information.
Note: this is a one-way operation. Once you eject
, you can’t go back!
If you aren’t satisfied with the build tool and configuration choices, you can eject
at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject
will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
You don’t have to ever use eject
. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
You can learn more in the Create React App documentation.
To learn React, check out the React documentation.
You can learn more web develop tools web.dev