React和useRoutes存在的问题

huangapple go评论64阅读模式
英文:

Problems with React and useRoutes

问题

我正在尝试使用React和useRoutes,但我遇到了一个错误,什么都没有显示出来。我无法弄清楚问题出在哪,你能帮助我吗?非常感谢

这是错误信息

routes.js:12 Uncaught TypeError: Cannot read properties of undefined (reading 'createElement')
    at routes.js:12:17
    ..

这些是我的文件

App.jsx 包含了BrowserRouter的导入部分

import Index from './Index.jsx';
import { BrowserRouter as Router, Link } from "react-router-dom";
import { Menu } from './Menu.jsx';

export const App = () => (
    <div>
        <Router>
            <Menu />
            <Index />
        </Router>
    </div>
);

Index.jsx 包含了useRoutes的部分

import { useRoutes } from "react-router-dom";
import { routes } from "./routes";

export const Component = () => {
  let element = useRoutes(routes);
  return element;
};

export default Component;

router.js 中声明了所有路由的地方

import React from "react";
import { Home } from './Home.jsx';
import { ErrorPage } from './ErrorPage.jsx';

export const routes = [
    {
        path:'/', 
        element:<Home />
    },
    { 
        path:'*',
        element:<ErrorPage /> 
    }
];

Menu.jsx 包含了站点菜单和链接

import React, { useState } from 'react';
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
import { pages } from '../../data/pages';

export const MenuTop = () => {
    const [myPages, setMyPages] = useState(pages);
    const [pageToDisplay, setPageToDisplay] = useState("/");

    const displayPage = (page) => { 
        setPageToDisplay(page.url);
    }

    return (
        <Router>
            <div className="ui menu">
                {myPages.map((el) => {
                    const { id, name, url } = el;
                    return (
                        <Link to={url} key={id} className={`item ${pageToDisplay === url ? "active" : ""}`} onClick={() => displayPage({url})}>{name}</Link>
                    );
                })}
            </div>
        </Router> 
    );
}
英文:

I am trying to use React and useRoutes, but I get an error and nothing is displayed. I can't figure out where the problem is, can you help me? Thank you very much

This is the error

routes.js:12 Uncaught TypeError: Cannot read properties of undefined (reading &#39;createElement&#39;)
    at routes.js:12:17
    ..

These are my files

App.jsx contains the import of BrowserRouter

import Index from &#39;./Index.jsx&#39;;
import { BrowserRouter as Router, Link } from &quot;react-router-dom&quot;;
import { Menu } from &#39;./Menu.jsx&#39;;

export const App = () =&gt; (
    &lt;div&gt;
        &lt;Router&gt;
		    &lt;Menu /&gt;
			&lt;Index /&gt;
        &lt;/Router&gt;
    &lt;/div&gt;
);

Index.jsx containing the useRoutes

import { useRoutes } from &quot;react-router-dom&quot;;
import { routes } from &quot;./routes&quot;;

export const Component = () =&gt; {
  let element = useRoutes(routes);
  return element;
};

export default Component;

router.js where all routes will be declared

import { React } from &quot;react&quot;;
import { Home } from &#39;./Home.jsx&#39;;
import { ErrorPage } from &#39;./ErrorPage.jsx&#39;;

export const routes = [
    {
        path:&#39;/&#39;, 
        element:&lt;Home /&gt;
    },
    { 
        path:&#39;*&#39;,             
        element:&lt;ErrorPage /&gt; 
    }
];

Menu.jsx containing the site menu and consequently the Links

import React, { useState } from &#39;react&#39;;
import { BrowserRouter as Router, Routes, Route, Link } from &quot;react-router-dom&quot;;
import { pages } from &#39;../../data/pages&#39;;

export const MenuTop = () =&gt; {
    const [myPages, setMyPages] = useState(pages);
    const [pageToDisplay, setPageToDisplay] = useState(&quot;/&quot;);

    const displayPage = (page) =&gt; { 
        setPageToDisplay(page.url);
    }

    return (
        &lt;Router&gt;
            &lt;div className=&quot;ui menu&quot;&gt;
                {myPages.map((el) =&gt; {
                    const { id, name, url } = el;
                    return (
                        &lt;Link to={url} key={id} className={`item ${pageToDisplay == url ? &quot;active&quot; : &quot;&quot;}`} onClick={() =&gt; displayPage({url})}&gt;{name}&lt;/Link&gt;
                    );
                })}
            &lt;/div&gt;
        &lt;/Router&gt; 
    );
}

答案1

得分: 2

问题在于您导入了错误的React,使用了命名导出而不是默认导出。错误出现在router.js/routes.js文件中:

在第一行,import { React } from 'react'

将其更改为默认导出:

import React from "react";
import { Home } from './Home.jsx';
import { ErrorPage } from './ErrorPage.jsx';

export const routes = [
    {
        path:'/', 
        element:<Home />
    },
    { 
        path:'*',             
        element:<ErrorPage /> 
    }
];

这应该解决问题。

英文:

The issue is that you're importing the wrong React, using a named export instead of a default one. Here is the error, in the router.js/routes.js:

On the first line, import { React } from &#39;react&#39;

import { React } from &quot;react&quot;;
import { Home } from &#39;./Home.jsx&#39;;
import { ErrorPage } from &#39;./ErrorPage.jsx&#39;;

export const routes = [
    {
        path:&#39;/&#39;, 
        element:&lt;Home /&gt;
    },
    { 
        path:&#39;*&#39;,             
        element:&lt;ErrorPage /&gt; 
    }
];

Change it to a default export:

import React from &quot;react&quot;;
import { Home } from &#39;./Home.jsx&#39;;
import { ErrorPage } from &#39;./ErrorPage.jsx&#39;;

export const routes = [
    {
        path:&#39;/&#39;, 
        element:&lt;Home /&gt;
    },
    { 
        path:&#39;*&#39;,             
        element:&lt;ErrorPage /&gt; 
    }
];

This should fix the issue.

huangapple
  • 本文由 发表于 2023年2月6日 21:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定