我正在使用Reaction路由构建一个Reaction应用程序,并且在我的应用程序组件中收到错误"歧义间接导出:switch ".以下是我的代码:

import React from 'react';
import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom';
import { makeStyles } from '@material-ui/core/styles';
import { Card, CardMedia, CardContent, Typography, Grid } from '@material-ui/core';
import About from './About';

const useStyles = makeStyles((theme) => ({
  // ... other styles
}));

const App = () => {
  const classes = useStyles();

  return (
    <Router>
      <div>
        {/* Displaying the header on all pages */}
        <div className={classes.greeting}>
          <Typography variant="h1">Welcome to the Fractals website!</Typography>
        </div>

        <Switch>
          <Route exact path="/">
            {/* Displaying the home page with cards */}
            <Grid container justifyContent="center">
              {/* Card for the "Theory" page */}
              <Grid item xs={12} sm={6} md={4}>
                {/* Adding a Link component to navigate to the "Theory" page */}
                <Link to="/theory" style={{ textDecoration: 'none' }}>
                  {/* Rest of the card code remains the same */}
                </Link>
              </Grid>

              {/* Other cards follow the same pattern */}
              {/* ... */}
            </Grid>
          </Route>

          {/* Routes for the pages */}
          <Route path="/theory">
            {/* Component for the "Theory" page */}
            <Theory />
          </Route>
          <Route path="/serpinski">
            {/* Component for the "Sierpinski Triangle" page */}
            <Serpinski />
          </Route>
          <Route path="/mandelbrot">
            {/* Component for the "Mandelbrot Set" page */}
            <Mandelbrot />
          </Route>
        </Switch>
      </div>
    </Router>
  );
};

export default App;

我相信我已经正确地从react-router-dom导入了Switch,所以我不确定是什么问题.如何解决此错误并使我的应用程序组件与Reaction路由一起正常工作?

我已经更新了使用Routes组件而不是Switch组件的react-router-dom@6代码,但仍然存在呈现问题.

import React from 'react';
import {
  BrowserRouter as Router,
  Route,
  Routes,
  Link
} from 'react-router-dom';
import { makeStyles } from '@material-ui/core/styles';
import { Card, CardMedia, CardContent, Typography, Grid } from '@material-ui/core';
import Mandelbrot from './Mandelbrot';
import Serpinski from './Serpinski';
import Theory from './Theory';

// Create styles using Material-UI
const useStyles = makeStyles((theme) => ({
  // ... other styles
}));

const App = () => {
  const classes = useStyles();

  return (
    <Router>
      <div>
        {/* Display the header on all pages */}
        <div className={classes.greeting}>
          <Typography variant="h1">Welcome to the Fractals website!</Typography>
        </div>

        <Routes>
          <Route exact path="/">
            {/* Display the main page with cards */}
            <Grid container justifyContent="center">
              {/* Card for the "Theory" page */}
              <Grid item xs={12} sm={6} md={4}>
                {/* Add a Link component to navigate to the "Theory" page */}
                <Link to="/theory" style={{ textDecoration: 'none' }}>
                  {/* The rest of the card's code remains unchanged */}
                </Link>
              </Grid>

              {/* Other cards are similar */}
              {/* ... */}
            </Grid>
          </Route>

          {/* Routes for the pages */}
          <Route path="/theory">
            {/* The "Theory" page component */}
            <Theory />
          </Route>
          <Route path="/serpinski">
            {/* The "Serpinski Triangle" page component */}
            <Serpinski />
          </Route>
          <Route path="/mandelbrot">
            {/* The "Mandelbrot Set" page component */}
            <Mandelbrot />
          </Route>
        </Routes>
      </div>
    </Router>
  );
};

export default App;

推荐答案

您已经安装了react-router-dom@6.11.0,但是您要从react-router-dom导入Switch组件,这是RRDv5组件导出,并且在RRDv6中被Routes组件替换.

你基本上有两个 Select :

  1. 为要导入和使用的组件安装正确的react-router-dom版本.

    首次卸载react-routerreact-router-dom:npm uninstall --save react-router react-router-dom 安装最新的RRDv5依赖项:npm install --save react-router-dom@5

  2. Migrate your code to react-router-dom@6.

    导入Routes组件以替换Switch组件,并更新Route组件以在elementprops as JSX上渲染其内容.

    import React from 'react';
    import {
      BrowserRouter as Router,
      Route,
      Routes,
      Link
    } from 'react-router-dom';
    import { makeStyles } from '@material-ui/core/styles';
    import {
      Card,
      CardMedia,
      CardContent,
      Typography,
      Grid
    } from '@material-ui/core';
    import About from './About';
    
    const useStyles = makeStyles((theme) => ({
      // ... other styles
    }));
    
    const App = () => {
      const classes = useStyles();
    
      return (
        <Router>
          <div>
            {/* Displaying the header on all pages */}
            <div className={classes.greeting}>
              <Typography variant="h1">
                Welcome to the Fractals website!
              </Typography>
            </div>
    
            <Routes>
              <Route
                path="/"
                element={(
                  {/* Displaying the home page with cards */}
                  <Grid container justifyContent="center">
                    {/* Card for the "Theory" page */}
                    <Grid item xs={12} sm={6} md={4}>
                      {/* Adding a Link component to navigate
                        to the "Theory" page */}
                      <Link to="/theory" style={{ textDecoration: 'none' }}>
                        {/* Rest of the card code remains the same */}
                      </Link>
                    </Grid>
    
                    {/* Other cards follow the same pattern */}
                    {/* ... */}
                  </Grid>
                )}
              />
    
              {/* Routes for the pages */}
              {/* Component for the "Theory" page */}
              <Route path="/theory" element={<Theory />} />
    
              {/* Component for the "Sierpinski Triangle" page */}
              <Route path="/serpinski" element={<Serpinski />} />
    
              {/* Component for the "Mandelbrot Set" page */}
              <Route path="/mandelbrot" element={<Mandelbrot />} />
            </Routes>
          </div>
        </Router>
      );
    };
    
    export default App;
    

Javascript相关问答推荐

v-textfield的规则找不到数据中声明的元素

Toast函数找不到其dis

我可以后增量超过1(最好是内联)吗?

使用Apps Script缩短谷歌表中的URL?

除了在Angular 16中使用快照之外,什么是可行且更灵活的替代方案?

在NextJS中使用计时器循环逐个打开手风琴项目?

fetch在本地设置相同来源的cookie,但部署时相同的代码不会设置cookie

react 路由加载程序行为

函数返回与输入对象具有相同键的对象

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

Ember.js 5.4更新会话存储时如何更新组件变量

如何从HTML对话框中检索单选项组的值?

我在Django中的视图中遇到多值键错误

类构造函数不能在没有用With Router包装的情况下调用

在Java中寻找三次Bezier曲线上的点及其Angular

Eval vs函数()返回语义

为什么当我更新数据库时,我的所有组件都重新呈现?

如果NetSuite中为空,则限制筛选

React Refs不与高阶组件(HOC)中的动态生成组件一起工作

如何在Java脚本中并行运行for或任意循环的每次迭代