question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function components cannot be given refs.

See original GitHub issue

Hello! I’d like to move from styled-components to goober, but can’t fix one issue:

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Here is example: https://codesandbox.io/s/react-goober-forked-w3dh2?file=/src/index.js

i’ve tried many things, but console.log(ref) is always undefined, instead of el What i’m doing wrong?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
cristianbotecommented, Mar 8, 2021

Hi @Crisis2010, thanks for opening this issue.

Since goober is agnostic of your virtual dom, jsx, library of choice,styled supports a second argument that is the reference for your library forwardRef API. In your case you need to pass React.forwardRef as the second argument to the components that you need your ref to:

const Title = styled("h1", React.forwardRef)`
  font-weight: bold;
  color: dodgerblue;
`;

Let me know if this helps. Cheers!

1reaction
cristianbotecommented, Mar 22, 2021

Awesome work @mpkelly! Thanks for opening the PR.

As for the .attrs api, there’s no equivalent in goober. I believe there should be a hard line between Styled components and regular ones and I feel strange about .attrs 😄 it’s like a pre-processor for your props but shouldn’t that functionality exist inside the functional components instead? I don’t know, I really think we should keep these things separated.

Anyway in goober the equivalent would be to have a InputStyled that handles the styling and the main Input component handling the type and size. Like so:

import { styled } from "goober";

const InputStyled = styled("input")`
  border-radius: 3px;
  border: 1px solid palevioletred;
  display: block;
  margin: 0 0 1em;
  padding: ${(props) => props.padding};

  ::placeholder {
    color: palevioletred;
  }
`;

const Input = ({ small, ...props }) => {
  return <InputStyled type="text" size={small ? 5 : undefined} {...props} />;
};

Running the above in here https://codesandbox.io/s/loving-gareth-n3vtx?file=/src/App.js:23-403

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I avoid 'Function components cannot be given refs ...
Just give it as innerRef , // Client.js <Input innerRef={inputRef} />. Use it as ref . // Input.js const Input = ({ innerRef...
Read more >
Solved: Function components cannot be given refs. ...
It will not generate an error of Function components cannot be given refs. /* Child.jsx */ import React from 'react' class Child extends...
Read more >
ref prop should not be used on React function components
This rule applies when a ref prop is specified on a React function component. The ref prop is used to provide a reference...
Read more >
Forwarding Refs
Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited...
Read more >
warning: function components cannot be given refs. ...
Iget error 'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?' . How...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found