vue中:key 和react 中key={}

为了给 vue 或者react 一个提示,以便它能跟踪每个节点的身份,从而重用和重新排序现有元素,你需要为每项提供一个唯一 key 属性

一句话概括就是key的作用主要是为了高效的更新虚拟DOM

ref的特性

React的ref有3种用法:

  • 字符串(已废弃)
  • 回调函数
  • React.createRef() (React16.3提供)

1. 字符串

最早的ref用法。

1.dom节点上使用,通过this.refs[refName]来引用真实的dom节点

//this.refs['inputRef']来访问

2.类组件上使用,通过this.refs[refName]来引用组件的实例

//this.refs['comRef']来访问

2. 回调函数

回调函数就是在dom节点或组件上挂载函数,函数的入参是dom节点或组件实例,达到的效果与字符串形式是一样的,

都是获取其引用。

回调函数的触发时机:

  1. 组件渲染后,即componentDidMount后
  2. 组件卸载后,即componentWillMount后,此时,入参为null
  3. ref改变后

    1.dom节点上使用回调函数

    <input ref={(input) => {this.textInput = input;}} type="text" />

    2.类组件上使用

    <CustomInput ref={(input) => {this.textInput = input;}} />

    3.可用通过props跨级传递的方式来获取子孙级dom节点或组件实例

3.React.createRefclass Child extends React.Component{

constructor(props){
super(props);
this.myRef=React.createRef();
}
componentDidMount(){
console.log(this.myRef.current);
}
render(){
return <input ref={this.myRef}/>
}

}

4.React.forwardRef

同样是React 16.3版本后提供的,可以用来创建子组件,以传递ref。

//子组件(通过forwardRef方法创建)

const Child=React.forwardRef((props,ref)=>(



));

//父组件

class Father extends React.Component{

constructor(props){

super(props);

this.myRef=React.createRef();

}

componentDidMount(){

console.log(this.myRef.current);

}

render(){

return

}

}

//生成高阶组件

const logProps=logProps(Child);

//调用高阶组件

class Father extends React.Component{

constructor(props){

super(props);

this.myRef=React.createRef();

}

componentDidMount(){

console.log(this.myRef.current);

}

render(){

return

}

}

//HOC

function logProps(Component) {

class LogProps extends React.Component {

componentDidUpdate(prevProps) {

console.log('old props:', prevProps);

console.log('new props:', this.props);

}

render() {
const {forwardedRef, ...rest} = this.props; // Assign the custom prop "forwardedRef" as a ref
return <Component ref={forwardedRef} {...rest} />;
}

}

// Note the second param "ref" provided by React.forwardRef.

// We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"

// And it can then be attached to the Component.

return React.forwardRef((props, ref) => {

return <LogProps {...props} forwardedRef={ref} />;

});

}

//生成高阶组件

const logProps=logProps(Child);

//调用高阶组件

class Father extends React.Component{

constructor(props){

super(props);

this.myRef=React.createRef();

}

componentDidMount(){

console.log(this.myRef.current);

}

render(){

return

}

}

//HOC

function logProps(Component) {

class LogProps extends React.Component {

componentDidUpdate(prevProps) {

console.log('old props:', prevProps);

console.log('new props:', this.props);

}

render() {
const {forwardedRef, ...rest} = this.props; // Assign the custom prop "forwardedRef" as a ref
return <Component ref={forwardedRef} {...rest} />;
}

}

// Note the second param "ref" provided by React.forwardRef.

// We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"

// And it can then be attached to the Component.

return React.forwardRef((props, ref) => {

return <LogProps {...props} forwardedRef={ref} />;

});

}

整理的有点乱,详细参考:

https://blog.csdn.net/liangklfang/article/details/72858295

https://blog.csdn.net/liwusen/article/details/80009968

vue中:key 和react 中key={} 的作用,以及ref的特性?的更多相关文章

  1. React中的Keys

    前言 当你在React当中渲染列表项的时候,React会尝试存储对应每个单独项的相关信息,如果你的组件包含state状态数据,那么这些状态数据必须被排序. 当你想要更新这些列表项的时候,React必须 ...

  2. 从 Vue 的视角学 React(二)—— 基本语法

    基于 Vue.js 开发的时候,每个 vue 文件都是一个单独的组件,可以包含 HTML,JS,CSS 而 React 是以函数为基础,每个 function 就是一个组件.虽然 JSX 让 HTML ...

  3. 谈谈Vue/React中的虚拟DOM(vDOM)与Key值

    谈谈Vue/React中的虚拟DOM(vDOM)与Key值 一.DocumentFragment 在了解虚拟DOM前,先来了解DOM的一个对象属性--DocumentFragment. 在一次操作中, ...

  4. React 中的key值

    在react中必须要有key值,key不是用来提升react的性能的,react中的key属性,它是一个特殊的属性,它是出现不是给开发者用的(例如你为一个组件设置key之后不能获取组件的这个key p ...

  5. react中对于key值的理解

    1.key是用来帮助react识别哪些内容被更改.添加或者删除.key需要写在用数组渲染出来的元素内部,并且需要赋予其一个稳定的值.如果key值发生了变更,react则会触发UI的重渲染. 2.在相邻 ...

  6. React中key的必要性与使用

    React这个框架的核心思想是,将页面分割成一个个组件,一个组件还可能嵌套更小的组件,每个组件有自己的数据(属性/状态);当某个组件的数据发生变化时,更新该组件部分的视图.更新的过程是由数据驱动的,新 ...

  7. 第二章 Vue快速入门-- 18 v-for中key的使用注意事项

    注意:如果属性和方法还没定义直接使用的话,就会报   xxx is not defined 导致界面不能正常显示.我看视频教程里老师的可以直接使用,而且界面正常显示,可能是vue版本不同吗?还不清楚 ...

  8. 【react】---react中key值的作用

    一.React中key值得作用 react中的key属性,它是一个特殊的属性,它是出现不是给开发者用的,而是给React自己使用,有了key属性后,就可以与组件建立了一种对应关系,简单说,react利 ...

  9. React中key的讲解

    通过阅读React的文档我们知道React这个框架的核心思想是,将页面分割成一个个组件,一个组件还可能嵌套更小的组件,每个组件有自己的数据(属性/状态);当某个组件的数据发生变化时,更新该组件部分的视 ...

随机推荐

  1. postgis常用的函数

    常见函数:http://postgis.net/docs/reference.html ST_GeometryType(geometry)    ——    返回几何图形的类型 ST_NDims(ge ...

  2. SQL必知必会——思维导图

    Xmind实在太坑了,竟然不能导出高清图片,我回来折腾个PS整一下!

  3. MySQL - Lock wait timeout exceeded

    今天突然出了个奇怪的问题,原本正常启动的项目,在什么都没有修改的情况下,启动到一半的时候会卡住几分钟,几分钟后又成功启动了,刚好是卡在Quartz那里,还以为出什么奇奇怪怪的幺蛾子了,一看日志,数据库 ...

  4. PHP中unset和null的比较

    起因 因为感兴趣于unset($var)和$var=null的区别,于是找了一个stackoverflow高分问题及答案,翻译以供参考. 注:以下的问题和答案翻译自http://stackoverfl ...

  5. VS2013+Opencv3.3配置教程

    转载自: https://blog.csdn.net/u014797226/article/details/78283873?locationNum=5&fps=1 参考博文1: 操作环境: ...

  6. Codeforces 1149C 线段树 LCA

    题意:给你一个括号序列,这个括号序列将确定一颗二叉树.有q次询问,每次询问输出这颗树的直径. 思路:https://blog.csdn.net/Huah_2018/article/details/89 ...

  7. redis配置篇

    配置 Redis的配置信息在/etc/redis/redis.conf下. 查看 sudo vi /etc/redis/redis.conf 核心配置选项 绑定ip:如果需要远程访问,可将此⾏注释,或 ...

  8. SDK打开模拟器遇到SDK包里缺少API组件,附上我的解决历程,心累

    背景描述:之前一直用真机做自动化,突然被要求用模拟器,就开始准备环境,发现模拟器里少很多配置,前提:配置了Android环境变量,且配置了代理如下:大连东软信息学院镜像服务器地址:http://mir ...

  9. Oracle实现主键自增的几种方式

    数据库作为一个系统的核心,数据库设计的1NF就是一个表结构必须有唯一约束也就是主键,Oracle数据库本身没有自增机制,不像MySQL直接使用关键字AUTO_INCREMENT自动加一,所以需要我们去 ...

  10. Kafka速览

    一.基本结构 三台机器组成的Kafka集群,每台机器启动一个Kafka进程,即Broker 向broker发送消息的客户端是Producer,拉取消息的客户端是Consumer Producer和Co ...