在本文中,您将会了解到关于reactjs–使用React-Router,每页有一个布局页面或多个组件的新资讯,同时我们还将为您解释createreactapp多页面的相关在本文中,我们将带你探索rea
在本文中,您将会了解到关于reactjs – 使用React-Router,每页有一个布局页面或多个组件的新资讯,同时我们还将为您解释create react app 多页面的相关在本文中,我们将带你探索reactjs – 使用React-Router,每页有一个布局页面或多个组件的奥秘,分析create react app 多页面的特点,并给出一些关于./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”、React Router 链接在使用 react-router-dom 的 ReactJS 和 Mobx 中的 Menu.Item 元素中不起作用、react 使用react-router-dom 在Route对象上component 参数接收的是一个方法而非一个对象、react-router – 使用React-Semantic-UI CardGroups配置React Router SubRoutes的实用技巧。
本文目录一览:- reactjs – 使用React-Router,每页有一个布局页面或多个组件(create react app 多页面)
- ./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”
- React Router 链接在使用 react-router-dom 的 ReactJS 和 Mobx 中的 Menu.Item 元素中不起作用
- react 使用react-router-dom 在Route对象上component 参数接收的是一个方法而非一个对象
- react-router – 使用React-Semantic-UI CardGroups配置React Router SubRoutes
reactjs – 使用React-Router,每页有一个布局页面或多个组件(create react app 多页面)
目前,一个模型被传递给一个根组件,它包含一个用于子导航的导航组件和一个主组件。
反应路由器的例子我发现只有一个子组件,什么是最好的方式有多个子组件更改,而不重复布局代码在两个?
// think of it outside the context of the router,if you had pluggable // portions of your `render`,you might do it like this <App children={{main: <Users/>,sidebar: <UseRSSidebar/>}}/> // So with the router it looks like this: const routes = ( <Route component={App}> <Route path="groups" components={{main: Groups,sidebar: GroupsSidebar}}/> <Route path="users" components={{main: Users,sidebar: UseRSSidebar}}> <Route path="users/:userId" component={Profile}/> </Route> </Route> ) class App extends React.Component { render () { const { main,sidebar } = this.props; return ( <div> <div className="Main"> {main} </div> <div className="Sidebar"> {sidebar} </div> </div> ) } } class Users extends React.Component { render () { return ( <div> {/* if at "/users/123" `children` will be <Profile> */} {/* UseRSSidebar will also get <Profile> as this.props.children,so its a little weird,but you can decide which one wants to continue with the nesting */} {this.props.children} </div> ) } }
也检查了sidebar example app,应该帮助你更多。
编辑:
根据@ Luiz的评论:
In the latest version of router the components are in the root of the props object
所以:
const { main,sidebar } = this.props.children;
变为:
const { main,sidebar } = this.props;
./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”
如何解决./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”
./node_modules/react-router-dom/react-router-dom.js Attempted import error: ''Navigate'' is not exported from ''react-router''.
react-router-dom的版本是6.0.0-alpha.2,而react-router是5.2.0。 两者均已正确安装。我不确定如何解决此错误。有人可以给我任何可能的解决方法吗?
我的代码中甚至没有<Navigate to=?>
行。
解决方法
为什么只安装两个都需要,这可以工作
- 执行npm删除react-router
- 删除node_modules
- 纱线安装或npm安装和
- 启动纱线或启动npm
React Router 链接在使用 react-router-dom 的 ReactJS 和 Mobx 中的 Menu.Item 元素中不起作用
如何解决React Router 链接在使用 react-router-dom 的 ReactJS 和 Mobx 中的 Menu.Item 元素中不起作用
我正在尝试使用 react-router-dom 在 React 应用程序中进行路由,但是当我尝试在菜单项元素中放置链接时,它似乎根本没有响应。
下面是navbar.tsx的代码
import React,{ useContext } from "react";
import { Menu,Container,Button } from "semantic-ui-react";
import ActivityStore from "../app/stores/activityStore";
import { observer } from "mobx-react-lite";
import { Link } from "react-router-dom";
const NavBar: React.FC = () => {
const activityStore = useContext(ActivityStore);
return (
<Menu fixed="top" inverted>
<Container>
<Menu.Item header as={Link} to=''/''}>
<img src="/assets/logo.png" alt="logo" style={{ marginRight: 10 }} />
Application Home Page
</Menu.Item>
<Menu.Item name="Activities" as={Link} to=''/activities'' />
<Menu.Item>
<Button
positive
content="Create Activity"
as={Link}
to=''/createActivity''
/>
</Menu.Item>
</Container>
</Menu>
);
};
export default observer(NavBar);
这是我定义路由的主要布局 App.tsx 页面
import { useEffect,Fragment,useContext } from "react";
import { Container } from "semantic-ui-react";
import NavBar from "../../features/nav/NavBar";
import ActivityDashboard from "../../features/activities/dashboard/ActivityDashboard";
import LoadingComponent from "./LoadingComponent";
import ActivityStore from "../stores/activityStore";
import { Route } from "react-router-dom";
import HomePage from "../../features/home/HomePage";
import ActivityForm from "../../features/activities/form/ActivityForm";
import { observer } from "mobx-react-lite";
const App = () => {
const activityStore = useContext(ActivityStore);
useEffect(() => {
activityStore.loadActivities();
},[activityStore]);
if (activityStore.loadingInitial)
return <LoadingComponent content="Loading activities" />;
return (
<Fragment>
<NavBar />
<Container style={{ marginTop: "7em" }}>
<Route exact path="/" component={HomePage} />
<Route path="/activities" component={ActivityDashboard} />
<Route path="/createActivity" component={ActivityForm} />
</Container>
</Fragment>
);
};
export default observer(App);
所以问题是我做错了什么?
编辑:
我通过用 Switch 包围路由组件解决了这个问题
<Switch>
<Container style={{ marginTop: "7em" }}>
<Route exact path="/" component={HomePage} />
<Route path="/activities" component={ActivityDashboard} />
<Route path="/createActivity" component={ActivityForm} />
</Container>
</Switch>
谢谢。
react 使用react-router-dom 在Route对象上component 参数接收的是一个方法而非一个对象
其实对于jsx语法 一直觉的它有点清晰都不是很好,js和html混在一起有点不伦不类的样子,以下是我在使用react中遇到的一个很奇葩的事情
假定你定义了一个component Mine
1 import React from ''react'';
2
3 class Mine extends React.Component {
4 constructor(peops) {
5 super();
6 }
7
8 render() {
9 console.log(''mine'', this);
10 return (
11 <div>
12 <div className=''head''>
13 <span>mine</span>
14 </div>
15 </div>
16 )
17 }
18 }
19
20 export {Mine}
然后你在另一个组件上引用
import React from ''react''
import {Mine} from "../mine/mine";
class San extends React.Component {
constructor(props) {
super();
this.state = {
name: ''第2个页面''
}
}
componentDidMount() {
console.log(typeof <Mine/>)//打印
console.log(typeof Mine) //打印
}
render() {
return (
<div id={''san''}>
{this.state.name}
</div>
)
}
}
export {San}
你会发现第一个<Mine/>输出的是一个对象 而Mine输出的是一个方法 而在react-router-dom中使用
return (
<HashRouter>
<Switch>
<Route exact path={''/''} component={Mine}/> //没有问题
<Route exact path={''/''} component={<Mine/>}/> //报错
<Route exact path={''/mine2''} component={() => Mine;
}/> //报错
<Route exact path={''/mine2''} component={() => <Mine/>;
}/> //没有问题
<Route exact path={''/mine2''} component={() => new Mine();
}/> //没有问题
</Switch> </HashRouter> )
其原因就component 接收的是一个方法而不是一个对象 而这个方法返回的值必须是react组件;
react-router – 使用React-Semantic-UI CardGroups配置React Router SubRoutes
This issue on github带我到了semantic-ui-react documentation on Augmentation,
以及setting up route config with sub-routes上的react-router文档.
我的路由配置似乎有效,但目前还不清楚具体实现组件扩充的位置.
我试过< Card.Group items = {items} as = {Link} to = {items.map((item,i)=>(item.href))} /> ;,以及试图把道具本身中的as = {Link},并且遇到了一堆错误,最终递归失败导致可怕的最大调用堆栈大小超出!
现在,我只是使用react-router对组件的路径进行硬编码.功能性方法会更优雅,反应更灵敏,这就是我所追求的!
因此,子组件API的正确用法是:
<Card.Group> <Card as={Link} to='/foo' /> <Card as={Link} to='/bar' /> </Card.Group>
正确使用速记API的方法是:
const items = [ { as: Link,content: 'Foo',to: '/foo' },{ as: Link,content: 'Bar',to: '/bar' },] <Card.Group items={items} />
关于reactjs – 使用React-Router,每页有一个布局页面或多个组件和create react app 多页面的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于./node_modules/react-router-dom/react-router-dom.js尝试导入错误:未从“ react-router”导出“ Navigate”、React Router 链接在使用 react-router-dom 的 ReactJS 和 Mobx 中的 Menu.Item 元素中不起作用、react 使用react-router-dom 在Route对象上component 参数接收的是一个方法而非一个对象、react-router – 使用React-Semantic-UI CardGroups配置React Router SubRoutes的相关知识,请在本站寻找。
本文标签: