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

150
Visualizações
How to make a wrapper interchangable?

I am facing a problem with my formik fields and need to use FastFields in some situations and Fields in others. Therefor my FormikControl, a class for building formik inputs needs to switch between and as wrappers. I am not sure how to do this. Here is part of the class:

const FormikControl = (props) => {
  const {
    name,
    type,
    label,
    disabled,
    fullWidth,
    isDynamic, // Field (true) or FastField
  } = props;
  const classes = useStyles();

  const inputProps = {};

  switch (type) {
    case 'text':
      return (
        <>
          <ComplexField name={name} isDynamic={isDynamic} {...props}> // meant to be the wrapper that switches
            {({ field }) => {
              return (
                <TextField
                  {...field}
                  variant="standard"
                  label={label}
                  fullWidth={fullWidth}
                  disabled={disabled}
                  InputLabelProps={{
                    shrink: true,
                  }}
                  InputProps={inputProps}
                  value={field.value || ''}
                />
              );
            }}
          </ComplexField>
          <ErrorMessage error name={name} component={FormHelperText} />
        </>
      ); 

ComplexField is meant to be the flexable wrapper and needs be able to switch between Field and FastField based on "isDynamic". My best try was this:

const ComplexField = ({ name, children, isDynamic, ...props }) => {
  return isDynamic ? (
    <Field name={name} props={props}>{({ field }) => children({ ...props, field })}</Field>
  ) : (
    <FastField name={name} props={props}>{({ field }) => children({ ...props, field })}</FastField>
  );
};

Didn't work :(

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

<ComplexField name={name}>

You're not passing there the prop "isDynamic" Is it intentional? Because like that it's obvious that doesnt work

<ComplexField name={name} isDynamic={isDynamic}>

If you want to use a better pattern, read that Conditionally render react components

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