# Monorepo Setup for Create React App and Components (2/4)

- URL: https://www.heissenberger.at/en/blog/monorepo-setup-javascript-typescript-react-npm-2/
- Language: en
- Published: 2021-02-04
- Tags: React JS, Monorepo, Lerna, Javascript, NPM

> Two independent Create React App projects which share a Component libary

This is the second part of a serie which provides answers on setting up a fullstack monorepo.
If need help on how to initialize a monorepo, have a look into part 1.

1. [Setup a Monorepo for Javascript backend services](https://www.heissenberger.at/en/blog/monorepo-setup-javascript-typescript-react-npm-1/)
2. **Add a Create React App frontend with a component libary** (current)
3. [Support Typescript in packages](https://www.heissenberger.at/en/blog/monorepo-setup-javascript-typescript-react-npm-3/)
4. [Deploy to private Github NPM registry](https://www.heissenberger.at/en/blog/monorepo-setup-javascript-typescript-react-npm-4/)

<br/><br />

## Goals

- Website which lists events
- Backend service with Login to create new events

<br/><br />

## Create the Website

we will use [**Create React App**](https://create-react-app.dev) to create the website:

```bash
cd services
yarn create react-app web
```

change the default port to 3001:

```bash
echo "PORT=3001" > web/.env
```

<br/><br />

## Create the Backend Web Application

we will use [**Create React App**](https://create-react-app.dev) again:

```bash
cd services
yarn create react-app admin
```

change the default port to 3002:

```bash
echo "PORT=3002" > admin/.env
```

<br/><br />

## Create the shared React Comonents Libary

```bash
mkdir -p packages/ekzemplo-components/src
cd packages/ekzemplo-components
yarn init -y
yarn add react --peer
```

**Hint: ** `--peer` will add as a requirement but does not add it to the libary

Modify the new `package.json`:

- set `version` to `0.0.0``
- change `main` to `./dist/index.js`
- add `module` and `source`
- add `scripts`

```json
{
  "name": "ekzemplo-components",
  "version": "0.0.0",
  "main": "./dist/index.js",
  "module": "./dist/index.js",
  "source": "./src/index.js",
  "license": "MIT",
  "scripts": {
    "start": "microbundle watch --jsx React.createElement",
    "build": "microbundle build --jsx React.createElement"
  },
  "peerDependencies": {
    "react": "^17.0.1"
  }
}
```

**Important:** the extentions for `"module": "./dist/index.js"` has to be `.js` - using `.mjs` will break `create-react-app`

add `microbundle` to transform and bundle the components:

```bash
yarn add --dev microbundle
```

create the `src/Event.jsx` component to show a event:

```jsx
import React from "react"
export default function Event({ name, date }) {
  return (
    <div>
      {date} - {name}
    </div>
  )
}
```

create the `src/EventList.jsx` component to show a event:

```jsx
import React from "react"
import { useQuery } from "react-query"
import Event from "./Event"
2
export default function EventList() {
  const { isLoading, error, data } = useQuery("repoData", () =>
    fetch("http://localhost:3010/events").then(res => res.json()),
  )

  if (isLoading) return "Loading..."

  if (error) return "An error has occurred: " + error.message

  return (
    <ul>
      {data.map(e => (
        <li key={e.id}>
          <Event name={e.name} date={e.date} />
        </li>
      ))}
    </ul>
  )
}
```

add the `react-query` to peerDependencies:

```bash
yarn add react-query --peer
```

add the `src/index.js`

```javascript
import Event from "./Event"
import EventList from "./EventList"

export { Event, EventList }
```

transform all components

```bash
yarn build
```

switch back to the frontend:

```bash
cd ../../services/web
```

First, install http-proxy-middleware:

```bash
yarn add http-proxy-middleware
```

Next, create `src/setupProxy.js` to route all call to `/events` to the `events microservice`:

```javascript
const { createProxyMiddleware } = require("http-proxy-middleware")

module.exports = function (app) {
  app.use(
    "/events",
    createProxyMiddleware({
      target: "http://localhost:3010",
      changeOrigin: true,
    }),
  )
}
```

<br/><br/>

**next part: ** [Support Typescript in packages](https://www.heissenberger.at/en/blog/monorepo-setup-javascript-typescript-react-npm-3/)
