【发布时间】:2019-11-03 09:39:53
【问题描述】:
我正在使用 styled-components、FontAwesome 和 React Hooks 为标题/导航栏创建图标和标签单元 (Box.js)。在 Box 内有一个 Container,其中包含 Icon 和 Label。我希望Icon 和Label 在您将鼠标悬停在其中任何一个 上时一起更改颜色,这就是为什么我将onMouseOver 和onMouseOut 放在Container 组件上的原因。我松散地关注this guide 来实现类似的东西。
在点击链接之前,我尝试了这个 useHover 钩子,它没有给我错误消息但没有产生任何效果(当我悬停在上面时,甚至没有 console.log 输出)。
我已经用谷歌搜索了错误消息,但看起来 Github 上现有的 StackOverflow 帖子和 React 问题没有处理 Hooks。其中报告的错误消息也略有不同:Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.我的错误消息没有“无状态”一词。
这里的工作示例:https://codesandbox.io/s/q7rwe
预期:当鼠标悬停在任一子元素上时,Container(即Icon 和Label)的所有子元素都变为绿色。
实际:我收到以下错误消息 -
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of `Box`.
in Icon (created by Box)
in div (created by Context.Consumer)
in StyledComponent (created by styled.div)
in styled.div (created by Box)
in Box (created by App)
in div (created by App)
in App
【问题讨论】:
-
这就是 forwardRef 的样子,以防万一codesandbox.io/embed/useref-error-7tere。问题是 FontAwesomeIcon 不支持 ref。有一个 pull request 应该会在合并后尽快解决此问题。
-
感谢@AlexanderStaroselsky 的链接!不知道 FontAwesome 问题。
标签: reactjs font-awesome react-hooks styled-components