Function components cannot be given refs和useref的current一直是undefined的解决办法

刚开始的网站报错

我当时依据这个错误Function components cannot be given refs全网搜了一晚上没任何头绪,你是不是也在想是react的useRef()哪里写错了,然后深夜十二点仔细核对了几遍csdn的使用的模板代码,还是没有解决方案。
最开始的网站报错

  • 这是reactRef标准使用
//父组件(组件头我就没写啦  标准头就行)
const markdownRef =useRef(null)//使用useref()获得子组件内容
console.log(markdownRef)
console.log(props)
const onClick = () => {
        console.log(markdownRef)
        console.log(props.write)
    }

return (
        <>
              //使用ref将子组件内容引出
     <Markdown ref={markdownRef}/>
        </>
    );




//子组件
  useImperativeHandle(ref, () => ({//使用react的useImperativeHandle将需要传给父组件的东西抛出(ref必传)
        onSave,
        text,
        token
 }))

转变思路开始觉得是useref的current一直是undefined的问题

我和你一样也觉得Function components cannot be given refs已经无解了,想砸电脑了,然后发现打印的console.log(markdownRef)中的current属性一直为null或者undefined
在这里插入图片描述
然后我又开始以useref的current一直是undefined为关键词全网搜索,最后得出的结论为需要在子组件上加上forwardRef包裹才能使用子对父通信,然后我天真的又以为我可以了

const Md: React.FC = forwardRef((props: any, ref) => {
    //使用react的useImperativeHandle将需要传给父组件的东西抛出(ref必传)用useImperativeHandle暴露一些外部ref能访问的属性
    useImperativeHandle(ref, () => ({
        onSave,
        text,
        token
    }))
    return <Editor
        modelValue={text}
        onChange={setText}
        onUploadImg={onUploadImg}
        theme={"light"}
        onSave={onSave}
        pageFullscreen={false}
        showCodeRowNumber={true}
        /*不知道怎么用这写
        格式错误方法
        sanitize={(html:string)=>(DOMPurify)};
         自定义工具栏
         defToolbars?: Array<ReactElement>;
         */
        codeStyleReverse={true}
        autoFocus={true}
        className={"markdown"}
    />;
})

可现实是Function components cannot be given refs依旧存在useref的current一直是undefined

开始醒悟,一定是connect的问题

React的高阶组件HOC,可以理解成在低阶组件上进行一些封装。高阶组件HOC如果不做一些特殊处理,是无法直接访问低阶组件实例的,要想通过ref访问低阶组件实例,调用connect时,需要传递参数{forwardRef : true}。

解决方案

export default connect(({write}) => ({write}),
    null,
    null,
    {forwardRef: true}
)(Md)

如果使用了高阶组件,请一定要这样写!!!!!不然你就会像我一样被折磨一天

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当我们在使用React函数组件时,可能会遇到一个错误信息:`Function components cannot be given refs`。这是因为函数组件本身是无状态的,没有实例可以被引用。 下面是一个解决这个问题的例子: ```javascript import React, { useRef, useEffect, forwardRef, useImperativeHandle } from 'react'; // 创建一个函数组件 const MyComponent = forwardRef((props, ref) => { // 在组件中使用 useRef 创建一个 ref const inputRef = useRef(null); // 使用 useImperativeHandle 将 ref 暴露给父组件 useImperativeHandle(ref, () => ({ focus: () => { // 在组件内部的方法中操作 inputRef inputRef.current.focus(); } })); return <input ref={inputRef} />; }); // 使用 MyComponent function App() { // 创建一个 ref const myComponentRef = useRef(null); useEffect(() => { // 在组件加载后,调用暴露的 focus 方法 myComponentRef.current.focus(); }, []); return ( <div> <h1>My App</h1> <MyComponent ref={myComponentRef} /> </div> ); } export default App; ``` 在上面的例子中,我们使用了`forwardRef`将函数组件转换为可接受`ref`的组件。然后使用`useRef`创建一个`inputRef`,并使用`useImperativeHandle`将`ref`暴露给父组件,通过定义`focus`方法来操作`inputRef.current`的`focus`方法。 在父组件中,我们创建一个`myComponentRef`来引用`MyComponent`,并使用`useEffect`在组件加载后调用`myComponentRef.current.focus()`来聚焦输入框。 通过这种方式,我们可以解决`Function components cannot be given refs`的问题,并在函数组件内部操作相应的DOM元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值