2022-07-12 15:42:08 +01:00
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- javascript
|
|
|
|
- react
|
|
|
|
---
|
|
|
|
|
|
|
|
# React application structure
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
When you use `create-react-app` it creates a default directory structure that is
|
|
|
|
a sensible one to follow:
|
2022-07-12 15:42:08 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
build/
|
|
|
|
public/
|
|
|
|
index.html
|
|
|
|
src/
|
|
|
|
index.js
|
|
|
|
App.js
|
|
|
|
```
|
|
|
|
|
|
|
|
## `public/`
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
- `public` constains the `index.html` file via which React connects to the
|
|
|
|
traditional DCOM. Within `index.html` you have `<div id="root">...</div>` as
|
|
|
|
the entry point where your application is injected into the standard DOM.
|
2022-07-12 15:42:08 +01:00
|
|
|
|
|
|
|
- Everything in this folder is as per standard web development practice.
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
- You shouldn't include React code here but you can paste necessary links and
|
|
|
|
metadata to the `head` of `index.html`
|
2022-07-12 15:42:08 +01:00
|
|
|
|
|
|
|
## `src/`
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
- It automatically contains `index.js` . This imports `React` and `ReactDOM` and
|
|
|
|
contains the `ReactDOM.render()` method:
|
2022-07-12 15:42:08 +01:00
|
|
|
```js
|
|
|
|
ReactDOM.render(
|
|
|
|
<App />
|
|
|
|
document.getElementById('root')
|
|
|
|
);
|
|
|
|
```
|
2024-02-02 15:58:13 +00:00
|
|
|
- The `App` component is your parent component and the container for your app.
|
|
|
|
It is where you will import your sub-components.
|
|
|
|
- If you are using React Router `App` becomes the index for the different page
|
2024-02-17 11:57:44 +00:00
|
|
|
components. See [Routing](Routing.md) for more detail.
|
2022-07-12 15:42:08 +01:00
|
|
|
|
|
|
|
## `build`
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
`build` houses the compiled version of your application, ready to be deployed to
|
|
|
|
a server. All contents are minified, compressed and optimised using Webpack.
|
2022-07-12 15:42:08 +01:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Once you have executed `npm run build` you can test the output by using `serve`
|
|
|
|
via the console:
|
2022-07-12 15:42:08 +01:00
|
|
|
|
|
|
|
```bash
|
|
|
|
serve -s build
|
|
|
|
```
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
`build` will comprise a minified version of `index.html` and a `static/`
|
|
|
|
directory which holds your compiled CSS and JS that `index.html` links to, as
|
|
|
|
per a standard static website.
|