React.createRef(),React.forwardRef(),forwardRef()结合next.js的link进行路由跳转

码云https://gitee.com/skyvilm/react-next.js

1.React.createRef()

作用:获取dom元素

使用

import React,{Component} from 'react'

export default class Index extends Componen{

constructor(props){

super(props)

this.myref=React.createRef(); //创建节点

}

componentDidMount(){

this.myref.current; //节点,React.createRef()会返回一个current对象,密闭性的

}

render(){

return <div ref={this.father}>

this is div

</div>

}

}

源码:

import type {RefObject} from 'shared/ReactTypes';//可修改value的 不可变的对象export function createRef(): RefObject {

//初始化ref对象,属性current初始值为null const refObject = {

current: null,

};

if (DEV) {

Object.seal(refObject); //表示密闭性,不可删除属性

}

return refObject;

}

2.React.forwardRef()

作用:

React.forwardRef 会创建一个React组件,这个组件能够将其接受的 ref 属性转发到其组件树下的另一个组件中;

使用:

import React from 'react'//funciton component是没有dom实例的,因为它是PureComponent,所以没有this,// 所以不能通过createRef()来拿到实例//将Father的father传给子组件,并绑定子组件的DOM实例,从而能在父组件拿到子组件的DOM实例const Child=React.forwardRef((props,ref)=>{

return <div ref={ref}>child div</div>

})export default class Index extends Componen{

constructor(props){

super(props)

this.father=React.createRef()

}

componentDidMount(){

this.father.current.value='test' }

render(){

return <Child ref={this.father} />

}

}

源码:

import warningWithoutStack from 'shared/warningWithoutStack';export default function forwardRef<Props, ElementType: React$ElementType>(

render: (props: Props, ref: ReactRef\) =\> ReactNode,

) {

//__DEV__可不看 if (DEV) {

if (render != null && render.$$typeof === REACT_MEMO_TYPE) {

warningWithoutStack(

false,

'forwardRef requires a render function but received a `memo` ' +

'component. Instead of forwardRef(memo(...)), use ' +

'memo(forwardRef(...)).',

);

} else if (typeof render !== 'function') {

warningWithoutStack(

false,

'forwardRef requires a render function but was given %s.',

render === null ? 'null' : typeof render,

);

} else {

warningWithoutStack(

// Do not warn for 0 arguments because it could be due to usage of the 'arguments' object render.length === 0 || render.length === 2,

'forwardRef render functions accept exactly two parameters: props and ref. %s',

render.length === 1 ? 'Did you forget to use the ref parameter?' : 'Any additional parameter will be undefined.',

);

}

if (render != null) {

warningWithoutStack(

render.defaultProps == null && render.propTypes == null,

'forwardRef render functions do not support propTypes or defaultProps. ' +

'Did you accidentally pass a React component?',

);

}

}

return {

//被forwardRef包裹后,组件内部的$$typeof是REACT_FORWARD_REF_TYPE $$typeof: REACT_FORWARD_REF_TYPE,

//render即包装的FunctionComponent,ClassComponent是不用forwardRef的 render,

};

}

解析

不看__DEV__的话,返回的也是一个Object,也就是说,Child被forwardRef包裹后,React.forwardRef(Child)的$$typeof是REACT_FORWARD_REF_TYPE

注意:

一旦在Father组件中,用JSX引用了Child组件,那么就是React.createElement(React.forwardRef(Child)),又包裹了一层,此时的$$typeof`是`REACT_ELEMENT_TYPE`,`type`是`React.forwardRef(Child)`,`type`里面的`$$typeof是REACT_FORWARD_REF_TYPE

3.React.forwardRef()结合next.js的link进行路由跳转

import React, { Component } from 'react';

import Link from 'next/link';

import Router from "next/router";

const RedLink=React.forwardRef((props,ref)=>{

return (

<a href={props.href} ref={ref}>子集组件</a>

)

})

class index extends Component {

render() {

return (

<div>

<h2>子集是组件的跳转,</h2>

<small>不知道为什么只能用a</small>

<Link href="/list" passHref>

<RedLink/>

</Link>

</div>

);

}

}

export default index;
最后编辑于:2024-10-01 17:41:54
© 著作权归作者所有,转载或内容合作请联系作者

喜欢的朋友记得点赞、收藏、关注哦!!!

相关推荐
MacroZheng3 分钟前
还在用WebSocket实现即时通讯?试试MQTT吧,真香!
java·spring boot·后端
midsummer_woo26 分钟前
基于springboot的IT技术交流和分享平台的设计与实现(源码+论文)
java·spring boot·后端
qq_三哥啊1 小时前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
别惹CC2 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
寒士obj2 小时前
Spring事物
java·spring
柯南二号3 小时前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
javachen__4 小时前
SpringBoot整合P6Spy实现全链路SQL监控
spring boot·后端·sql
IT毕设实战小研10 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
一只爱撸猫的程序猿11 小时前
使用Spring AI配合MCP(Model Context Protocol)构建一个"智能代码审查助手"
spring boot·aigc·ai编程
甄超锋12 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat