My Android Capacitor app does not render components in React Router Dom

I am building an android application with Typescript, CapacitorJS, React, React-Router-Dom, and BlueprintJS.

When I execute React application in the web browser environment, Every component which I have written is normally rendered.

After Building React App, I have installed node.js packages @capacitor/core@next @capacitor/cli@next @capacitor/android@next. I run commands “yarn cap init --web-dir=build”, “yarn cap add android”, and “yarn cap open android”.

I have built a node.js express backend API server with port 5000, I wrote the line, android:usesCleartextTraffic=“true” in AndroidManifest.xml. Capacitor App in Android would request to the localhost backend server, the API address that mobile client request has been changed “10.0.2.2:5000”.

In Android Studio, no errors found. After clicking the “Run” button, a mobile emulator turns on, and the capacitor app is executed.

In the mobile environment, I cannot see anything on the virtual device, except the component Header. The component Header is not in the Switch group.

How can I render the components in React Router Dom in the android environment?

Here are my codes, React App.tsx, package.json, and AndroidManifest.xml.

App.tsx

import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import Header from './components/Header';
import PostMain from './PostList';
import PostItem from './PostItem';
import SearchList from './SearchList';
import CommentEdit from './CommentEdit';
import CommentDelete from './CommentDelete';
import SourceList from './SourceList';

const App = () => {
  return (
    <Router>
      <div
        style={{
          maxWidth: '920px',
          width: '100%',
          margin: '0 auto 20px',
          padding: '0px',
        }}
      >
        <Header />
        <Switch>
          <Route exact path="/" component={PostMain} />
          <Route exact path="/id/:id" component={PostItem} />
          <Route exact path="/source/:id" component={SourceList} />
          <Route exact path="/search/:keyword" component={SearchList} />
          <Route exact path="/comments/edit/:id" component={CommentEdit} />
          <Route exact path="/comments/delete/:id" component={CommentDelete} />
        </Switch>
      </div>
    </Router>
  );
};

export default App;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.application">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/AppTheme">

        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="com.example.application.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>
    </application>

    <!-- Permissions -->

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
{
  "name": "EXAMPLE_APP_NAME",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@blueprintjs/core": "^3.44.0",
    "@capacitor/android": "^3.0.0-rc.0",
    "@capacitor/cli": "^3.0.0-rc.0",
    "@capacitor/core": "^3.0.0-rc.0",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@types/react-router-dom": "^5.1.7",
    "axios": "^0.21.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
1 Like