react 使用react-router-dom 在Route对象上component 参数接收的是一个方法而非一个对象
其实对于jsx语法 一直觉的它有点清晰都不是很好,js和html混在一起有点不伦不类的样子,以下是我在使用react中遇到的一个很奇葩的事情
假定你定义了一个component Mine
import React from 'react'; class Mine extends React.Component {
constructor(peops) {
super();
} render() {
console.log('mine', this);
return (
<div>
<div className='head'>
<span>mine</span>
</div>
</div>
)
}
} 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 使用react-router-dom 在Route对象上component 参数接收的是一个方法而非一个对象的更多相关文章
- React: 认识React
一.简介 React-Native是Facebook开源的跨平台框架,用于实现前端和原生进行混合开发.React-Native开发可以很好的使用原生UI构建用户界面,与传统的使用WebView相比,不 ...
- beforeRouteEnter 与 beforeRouteUpdate(watch $route 对象) 的区别
项目 区别 适用场景 网址 beforeRouteEnter beforeRouteEnter 守卫 不能 访问 this,因为守卫在导航确认前被调用,因此即将登场的新组件还没被创建.不过,你可以通过 ...
- React对比Vue(03 事件的对比,传递参数对比,事件对象,ref获取DOM节点,表单事件,键盘事件,约束非约束组件等)
import React from 'react'; class Baby extends React.Component { constructor (props) { super(props) t ...
- [React Router v4] Intercept Route Changes
If a user has entered some input, or the current Route is in a “dirty” state and we want to confirm ...
- [React] Create a Query Parameter Modal Route with React Router
Routes are some times better served as a modal. If you have a modal (like a login modal) that needs ...
- 六、React 键盘事件 表单事件 事件对象以及React中的ref获取dom节点 、React实现类似Vue的双向数据绑定
接:https://www.cnblogs.com/chenxi188/p/11782349.html 事件对象 .键盘事件. 表单事件 .ref获取dom节点.React实现类似vue双向数据绑定 ...
- [React] 10 - Tutorial: router
Ref: REACT JS TUTORIAL #6 - React Router & Intro to Single Page Apps with React JS Ref: REACT JS ...
- React v16-alpha 从virtual dom 到 dom 源码简读
一.物料准备 1.克隆react源码, github 地址:https://github.com/facebook/react.git 2.安装gulp 3.在react源码根目录下: $npm in ...
- react.js 从零开始(七)React (虚拟)DOM
React 元素 React 中最主要的类型就是 ReactElement.它有四个属性:type,props,key 和ref.它没有方法,并且原型上什么都没有. 可以通过 React.create ...
随机推荐
- ionic3 图片(轮播)预览 ionic-gallary-modal组件使用方法
一.效果展示 使用方法: 1.npm安装ionic-gallary-modal扩展模块 npm install ionic-gallery-modal --save 2.在app.module.ts根 ...
- Android开发环境部署:JDK+Android Studio
1. 刚开始接触Android开发,首先需要为你的电脑安装java JDK(Java开发工具包),不管是用Eclipse还是Android Studio都需要只吃Java语言运行吧. 官网:Oracl ...
- rabbitmq AmqpClient 使用Topic 交换机投递与接收消息,C++代码示例
// strUri = "amqp://guest:guest@192.168.30.11:8820/test" // strUri = "amqp://[帐户名]:[密 ...
- 72.Properties(配置文件)
Properties(配置文件):主要用于存储配置文件到硬盘上面和读取配置文件 public class Properties extends Hashtable<Object,Object&g ...
- USB之Main item, Local item和Global item 的作用范围与归类
https://doc.micrium.com/display/OSUM50300/USB+Device+HID+Class+Overview report descriptor –> item ...
- centos修改、保存文件的详细步骤
[一]修改文件 如果是使用普通用户登录的,需要先切换到管理员用户,打开终端,输入:su,接着按提示输入密码即可:然后使用命令进入需要修改文件的所在目录,常用的几个命令如下: ① cd + 目录名 ② ...
- Delphi内建异常类 异常处理参考
标签: delphiexceptionwindowscasting编程integer 2012-05-19 12:53 2579人阅读 评论(0) 收藏 举报 分类: Delphi(96) [详细过程 ...
- delphi 数据处理
TStringStream 怎样转换String procedure TForm1.Button1Click(Sender: TObject); var ss:TStringStream; str:S ...
- (1)sqlserver2017安装
本体 https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 图形管理工具ssm 文档 https://docs.microso ...
- 配置ssh连接会话复用免密码登录
我们经常使用ssh连接远程主机,为了方便,避免每次登录输入密码,通常使用密钥登录.如果没有设置密钥, 则需要使用密码登录了,若每次都输入密码则十分繁琐.我们可以设置ssh连接会话复用,则登录成功后,会 ...