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

143
Vistas
React Styled Components: ReferenceError: Cannot access before initialization

SharedButton is the base style of a button inside my app, and it is shared across components. I am trying to override the styles of this button inside MyComponent.

I am getting the following error when myComponent mounts: ReferenceError: Cannot access 'SharedButton' before initialization.

Note that If I don't import SharedButton from a different file, ie. if I define it inside "MyComponent", I don't get the error. How can I solve this issue?

shared.ts

import styled from "styled-components";

export const SharedButton = styled.button`
  border: 1px solid red;
  color: #1c101e;
  font-size: 14px;
`;

myComponent.ts

import { SharedButton } from "./shared";
import styled from "styled-components";

const MyComponentButton = styled(SharedButton)`
  float: right;
`;

const MyComponent = () => {
   return <MyComponentButton> TEST </MyComponentButton>
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try placing the MyComponentButton style in shared.ts and export to the myComponent.ts. sandbox examples

shared.ts

import styled from "styled-components";

export const SharedButton = styled.button`
  border: 1px solid red;
  color: #1c101e;
  font-size: 14px;
`;

export const MyComponentButton = styled(SharedButton)`
  float: right;
`;

myComponent.ts

import { MyComponentButton } from "./shared";

const MyComponent = () => {
   return <MyComponentButton> TEST </MyComponentButton>
}

The second approach, put some props in styled-components

shared.ts

import styled, { css } from "styled-components";

export const SharedButton = styled.button`
  border: 1px solid red;
  color: #1c101e;
  font-size: 14px;
  ${({ floatRight }) =>
    floatRight &&
    css`
      float: left;
    `}
`;

myComponent.ts

import { SharedButton } from "./shared";
    
export const MyComponent = () => {
  return <SharedButton floatRight> TEST </SharedButton>;
};
about 4 years ago · Juan Pablo Isaza 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