How to integrate React app into Wordpress (react router)?

think-serious

I've created a react app to embed to wordpress site. React app is created by create-react-app and after bundling, I've uploaded it to wordpress site. Then created a page to load the bundled js file. (following this guide https://medium.com/@CodeCareerCoach/react-app-inside-a-wordpress-page-or-post-4c7d38181b3d)

So it shows the react app and works for the base route.

When I navigate, it still works, when I refresh I get 404 ERR.

This is the routing of react app.

  return <BrowserRouter basename='/testweb'>
    <Layout menuItems={menuItems} pages={pages}>
      <Switch>
          <Route exact path="/" render={() => <div>okay</div>} />
          <Route exact path="/home" render={(props) => {
              return <div>asdf</div>
            }} 
          />
...
          <Route render={(props)=> props.location.pathname } />
      </Switch>
    </Layout>
  </BrowserRouter>

in package.json, I've set homepage to /testweb How can I get the nested routes working?

think-serious

I found my own answer. Please share a better one if you have.

Let's say our app's name is testweb.

  • First, changed homepage attribute in package.json to another,
    testweb-react.
  • Kept the routing as it is(using testweb as base url).
  • Uploaded build files to root of wordpress site.

  • Created /testweb/.htaccess file to internally redirect to testweb-react

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ – [L]
RewriteRule ^ /testweb-react/ [L]
</IfModule>

Hope this helps others also!

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I integrate a background JS animation into my react app?

分類Dev

How does react render multiple pages in app.js using react-router?

分類Dev

How to integrate django with react and webpack to create a multipage webapp?

分類Dev

React Router + AWS Backend, how to SEO

分類Dev

How to make forwarding react router 5

分類Dev

How to Use nested routes in react router dom

分類Dev

How to detect if react-router is set?

分類Dev

React router 6, useNavigate how to get pathname

分類Dev

How to implement a hook for componentDidMount with react-router?

分類Dev

How to combine react-router useRouterHistory with redux-simple-router

分類Dev

React-router issues with deploying github pages, with create-react-app

分類Dev

React router nested not working

分類Dev

React-Router:MemoryHistory

分類Dev

React Native Router Flux

分類Dev

React Router Errors

分類Dev

React Router Match with Params

分類Dev

react router lazy component

分類Dev

React router redirect

分類Dev

React.jsを使用したSSR(create-react-app、react-router v4、redux、sagas)

分類Dev

how to send email directly in React Native App

分類Dev

How to persisting data in a react/redux web app?

分類Dev

How to set up NetlifyCMS with a pure react app

分類Dev

How to rename the folder created by create react app?

分類Dev

react native how run code if uninstall app

分類Dev

How to implement skipWaiting with Create React App?

分類Dev

How to run the react app without a server

分類Dev

How to create and deploy a React App properly?

分類Dev

How to exclude global styles in a React App?

分類Dev

How to Embed Google Trends into React app

Related 関連記事

ホットタグ

アーカイブ