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
© 著作权归作者所有,转载或内容合作请联系作者

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

相关推荐
Yyyyyy~19 小时前
【Java】Day4Web基础知识
java·spring boot
逆回1 天前
升级 Boot 4 后,我的 mock 全红了
spring boot
学长毕设1 天前
基于SpringBoot的野生动物公益保护平台设计与实现-附源码
java·spring boot·后端
她的男孩1 天前
三人会签只批了一个就往下走:Forge Admin 把 all / any / ratio 写成 completionCondition
spring boot·低代码·工作流引擎
驭渊的小故事1 天前
Spring AOP 核心概念与通知类型详解
java·后端·spring
vipxieliang1 天前
ValidX vs 手写验证代码:开发效率对比实测
java·spring boot·后端
vipxieliang1 天前
虚拟线程与 GraalVM:Spring Boot 3.x 云原生性能跃迁
java·spring boot·后端
驭渊的小故事1 天前
Spring AOP 入门:从 AOP 概念到第一个耗时统计切面
java·后端·spring
头发还在的女程序员1 天前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
CoderYanger2 天前
Java EE 进阶:8.MyBatis 操作数据库(入门)(第二弹)
java·数据库·spring boot·spring·面试·java-ee·mybatis