简体   繁体   English

React.forwardRef导致样式组件出错

[英]React.forwardRef Causes Error with Styled Component

I am running React 16.3.1 and Styled Components 3.2.5 currently and am hitting an issue trying to use React.forwardRef. 我目前正在运行React 16.3.1和Styled Components 3.2.5,并试图使用React.forwardRef。

I have an Input component that is comprised of a wrapper div that holds the label and input field. 我有一个Input组件,它由一个包含标签和输入字段的包装div组成。 However, I want to be able to forward a ref directly to the input field and not have to traverse to it via the primary wrapping div. 但是,我希望能够将ref直接转发到输入字段,而不必通过主包装div遍历它。

const Input = React.forwardRef((props, ref) => (
  <Wrapper>
    <Label htmlFor={props.id} required={props.required}>
      {props.label}
    </Label>

    <InputField
      id={props.id}
      ...
    />
  </Wrapper>
));

That is a simplified version of my component. 这是我的组件的简化版本。 However, this creates the following error: 但是,这会产生以下错误:

Uncaught Error: Cannot create styled-component for component: [object Object]

Maybe upgrading Styled Components to v4 would help? 也许将Styled Components升级到v4会有帮助吗? But is there any solution before upgrading that I haven't found yet? 但升级之前是否有任何解决方案尚未找到?

Thank you. 谢谢。

As explained in related issue , there is blocking React Redux issue that is expected to be closed with this PR . 正如相关问题中所解释的那样,阻止React Redux问题 ,预计将通过此PR关闭。

A workaround is to use an approach that was used before React 16.3 forwardRef and use custom prop instead of ref to forward refs: 解决方法是使用在React 16.3 forwardRef之前使用的方法,并使用自定义prop而不是ref来转发refs:

const Input = ({forwardRef, ...props}) => (
  <Wrapper>
    <Label htmlFor={props.id} required={props.required}>
      {props.label}
    </Label>

    <InputField
      ref={forwardRef}
      id={props.id}
      ...
    />
  </Wrapper>
));

Why <Wrapper {...rest}> ? 为什么<Wrapper {...rest}>

I thing pass ref to Wrapper is a correct way: 我的事情传递给Wrapper是一个正确的方法:

<Wrapper ref={ref}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.forwardRef() - 解析错误:缺少分号 - React.forwardRef() - Parsing error: Missing semicolon 如何在基于 class 的组件中使用 React.forwardRef? - How to use React.forwardRef in a class based component? 如何在 React Native 中使用 React.forwardRef() - How to use React.forwardRef() in React Native 使用 React.forwardRef() 在 React 中调用子组件函数的正确方法是什么? - What is the right way to call child component functions in React using React.forwardRef()? 来自 React.forwardRef 的 ref 为空 - ref from React.forwardRef is null React.forwardRef 在流中具有通用的 Props 类型 - React.forwardRef with generic Props type in flow 我如何使用 jsdoc 注释注释 React.forwardRef 以便智能感知可以显示在底层组件中? - How do i annotate React.forwardRef with jsdoc comments so that intellisense can show up for the underlying component? React 样式组件在生产中导致构建错误,但在开发中运行良好 - React styled component causes build error in production, but runs fine in development ReactJS:React-Month-Picker 错误:函数组件不能有引用。 你的意思是使用 React.forwardRef() 吗? - ReactJS: React-Month-Picker Error: Function components cannot have refs. Did you mean to use React.forwardRef()? 在使用 forwardRef 创建的 React 组件上输入错误 - Type error on a React component created with forwardRef
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM