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

245
Visualizações
How to convert custom proptype in react jsx to typescript

I am converting my project files from jsx to tsx. For normal proptypes, I am able to provide equivalent in interface. But for custom Proptypes I am unable to do the same. please find below the example scenario to get an idea.

AppContainer.jsx

    const AppContainer = (props) => {
  return <div>{/*my component*/}</div>;
};

const customPropCheck = (props, propName, componentName) => {
  if (
    (props.primaryValue && !props.secondaryValue) ||
    (!props.primaryValue && props.secondaryValue)
  ) {
    return new Error(`Error for ${componentName} `);
  }
  return null;
};
AppContainer.defaultProps = {
  primaryValue: null,
  secondaryValue: null,
};

AppContainer.propTypes = {
  primaryValue: customPropCheck,
  secondaryValue: customPropCheck,
};

export default AppContainer;

AppContainer.tsx

interface AppContainerProps {
  primaryValue: Error | null;
  secondaryValue: Error | null;
}

const AppContainer = ({
  primaryValue = null,
  secondaryValue = null,
}: AppContainerProps) => {
  return <div>{/*my component*/}</div>;
};

export default AppContainer;

How do i implement customPropCheck in my tsx? Is there any way Or do I have to copy same propType implementation in my tsx file ie:

AppContainer.propTypes = {
    primaryValue: customPropCheck,
    secondaryValue: customPropCheck 
} 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

TypeScript can help with the type aspect of prop-types, but if you want custom checks like yours to be applied when the props are specified (instead of later when the component is rendered), you still need prop-types to do that.

As far as I can tell, there's no special problem using it. The types of the parameters in customPropCheck are AppContainerProps, keyof AppContainerProps, and string respectively.

const customPropCheck = (props: AppContainerProps, propName: keyof AppContainerProps, componentName: string) => {
    if (
        (props.primaryValue && !props.secondaryValue) ||
        (!props.primaryValue && props.secondaryValue)
    ) {
        return new Error(`Error for ${componentName} `);
    }
    return null;
};

Then you just assign to the function (I was expecting this to be a problem, but it doesn't seem to be):

AppContainer.defaultProps = {
    primaryValue: null,
    secondaryValue: null,
};

AppContainer.propTypes = {
    primaryValue: customPropCheck,
    secondaryValue: customPropCheck,
};

Playground link

The defaultProps part of that is conceptually redundant, you're specifying defaults in the destructuring in your component props, but again those will get applied once when the element is created, not repeatedly when the component for it gets rendered, so there might be an argument for keeping it (although your custom check doesn't rely on it, you'll see undefined instead of null but your checks don't care about the distinction).

about 4 years ago · Juan Pablo Isaza 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