DEV Community

Smeden
Smeden

Posted on

Static website with React app in subdirectory

I have a static website on www.mydomain.com with two pages:
home: www.mydomain.com
about: www.mydomain.com/about

How can I add a React app to www.mydomain.com/app?
It should use routing, so I e.g. can go to www.mydomain.com/app/profile

I host it on Cloudflare Pages.

Top comments (4)

Collapse
 
mexyx profile image
xu1xiang

Example:

import { BrowserRouter as Router } from 'react-router-dom'
import ReactDOM from 'react-dom'

ReactDOM.render(
  <Router basename="/app"><App /></Router>,
  rootElement
)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
smeden profile image
Smeden

Thank you xu1xiang,

Can you please elaborate on how the file structure should be?
Should it be somehing like:
file structure
And what should the build configuration be like? Eg. what should "Build output directory" be on Cloudflare?

Collapse
 
mexyx profile image
xu1xiang
Thread Thread
 
mexyx profile image
xu1xiang

I know how to solve your problem, but I'm sorry my English is not very good, I don't know how to describe to you in English, hope the above link can help you