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 ...
随机推荐
- 用户界面样式(cursor,resize,vertical-align,outline,文字超出显示省略号)
1. 鼠标样式 cursor default: 小白(箭头)默认 pointer:小手 move:移动 text:文本 not-allowed:禁止 2. 轮廓线(表单外发光)outline 给表单添 ...
- hbase GC优化
编辑配置文件 编辑 hbase-env.sh export HBASE_OPTS="$HBASE_OPTS -XX:+UseCompressedOops -XX:+UseParNewGC ...
- Dubbox服务的提供方开发
(1)创建Maven工程(WAR)dubboxdemo-service ,在pom.xml中引入依赖 <project xmlns="http://maven.apache.org/ ...
- VS2017编译64位CloudCompare
需求:编译一个支持读写las点云的CC,然后再开发CC插件实现业务功能. 编译环境: 1.Windows 10 2.Visual Studio 2017 Community 3.Qt 5.9.4 开源 ...
- leetcood学习笔记-9
题目描述 方法一:转换为字符串 class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False e ...
- SparkSQL的一些用法建议和Spark的性能优化
1.写在前面 Spark是专为大规模数据处理而设计的快速通用的计算引擎,在计算能力上优于MapReduce,被誉为第二代大数据计算框架引擎.Spark采用的是内存计算方式.Spark的四大核心是Spa ...
- hdu1848 Fibonacci again and again [组合游戏]
http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers) ...
- XSS的原理分析与解剖:第三章(技巧篇)**************未看*****************
0×01 前言: 关于前两节url: 第一章:http://www.freebuf.com/articles/web/40520.html 第二章:http://www.freebuf.com/a ...
- 【UR #2】跳蚤公路
[UR #2]跳蚤公路 参照yjc方法.也就是地铁环线那个题. 求每个点不在负环内的x的取值范围.然后所有1到j能到i的j的范围取交.得到答案. 每个边形如kx+b的直线,每个环也是 每个点不在负环内 ...
- NGINX配置之一:日志篇
打开nginx.conf配置文件: vi /usr/local/nginx/conf/nginx.conf 日志部分内容: 日志生成的到Nginx根目录logs/access.log文件,默认使用“m ...