Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

371
Visualizações
How to get rid off error Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

I am using "styled" from material ui to stylize my components. Now, im cleaning code from console errors.

At first, i was having such thing: "Warning: React does not recognize the constantColors prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase constantcolors instead. If you accidentally passed it from a parent component, remove it from the DOM element".

As for example, the issue was in passing all props to the DOM children element (knowned issue?) So, this code:

export const StyledAddCircleOutlined = styled(AddCircleOutline)(
  ({ theme, constantColors = false }) => css`
    color: ${constantColors ? theme.palette.success.main : 'none'};
    transition: all 1s ease;
    cursor: pointer;
    :hover {
      color: ${constantColors ? 'none' : theme.palette.success.main};
      opacity: ${constantColors ? 0.6 : 'none'};
    }
    :active {
      color: ${theme.palette.success.light};
      transition: all 5ms ease;
    }
  `
)

was refactored by me to such thing:

export const StyledAddCircleOutlined = styled(
  ({ theme, constantColors = false, ...rest }) => <AddCircleOutline {...rest} />
)(
  ({ theme, constantColors = false }) => css`
    color: ${constantColors ? theme.palette.success.main : 'none'};
    transition: all 1s ease;
    cursor: pointer; 

(...)

And in most cases this works on other components... but not this time. This time, i have just a new error instead :)

"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 Styled(Component)"

Whats going on ? Usually such refactor works, its not even a complex function, its just an Icon from material-ui library :P

How to fix that ? I am not giving anywhere refs and i dont known if I should ?

In DOM structure it looks like that:

          {index === 0 && !isMobileDevice && (
            <StyledAddCircleOutlined
              onClick={() => handleAddField(name, field.value)}
              constantColors
            />
          )}
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

when using styled-components, ref (and more) props are being passed by default. in your case, when you add the ...rest you are passing more props than you can see (print it and look at console).

just take the ref out of it like this: export const StyledAddCircleOutlined = styled( ({ theme, constantColors = false, ref, ...rest }) => <AddCircleOutline {...rest} />,

if this doesnt solve your problem, wrap your component with forwardRef and extract it as ref

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda