简体   繁体   English

在一个 React 页面上使用多个组件

[英]Use multiple components on one React Page

I am new to learning React and so far as I have been exploring its amazing.我是学习 React 的新手,到目前为止我一直在探索它的惊人之处。 I have a quick question which is how can I display multiple modules such as "app.js" and "About.js" on one page?我有一个简单的问题,即如何在一个页面上显示多个模块,例如“app.js”和“About.js”? is this possible?这可能吗?

Here is some images that may help.这里有一些可能有帮助的图片。

React App.js and About.js React App.js 和 About.js

React index.js反应 index.js

React js console error反应js控制台错误

Commonly in a react app, you will route based on different URLs.通常在 React 应用程序中,您将根据不同的 URL 进行路由。

The different 'modules' (also known as components) are typically defined in other JS files.不同的“模块”(也称为组件)通常在其他 JS 文件中定义。 See the HomePage and AboutPage files in the example below.请参阅以下示例中的 HomePage 和 AboutPage 文件。

First, import the other pages.首先,导入其他页面。 Second, route to them given a path.其次,给定路径给他们路由。

A common routing solution in React is React Router React 中一个常见的路由解决方案是React Router

import React from 'react';

import { BrowserRouter } from 'react-router-dom';

import HomePage from './pages/HomePage.js';
import AboutPage from './pages/AboutPage.js';

const MyApp = () => (
  <BrowserRouter>
    <Switch>
      <Route path="/home" component={HomePage} />
      <Route path="/about" component={AboutPage} />
    </Switch>
  </BrowserRouter>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM