Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

372
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda