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

79
Visualizações
Set a field based on another filed in Formik based on an Object

I have two input fields. I want to update one field based on another field and I am using Formik. When i type in address field it sets it into input state and calls handleChange and finds the pincode and logs it. But it is coming wrong as it is supposed to change whenever we type but it just sets the finds it for the first letter. Lets say for "h" 560102 and even though we type "hosurRoad" the pincode doesn't change. Also how do i update another field pincode with this value.

export const SignupForm = () => {
  const classes = useStyles();
  const [input, setInput] = useState("");

  const recoganizedAreas = {
    'hsr':'560102',
    'bypanahalli':'676667',
    'marathalli':'667776',
    'hosurRoad':'321236',
    'teachersColony':'560101'
  };
  
  useEffect(() => {
    const handleChange = () => {
      const foundArea = Object.keys(recoganizedAreas).find(key => key.includes(input))
  
      const foundPinCode = recoganizedAreas[foundArea] || "Not found";
      console.log(foundPinCode);
    }
  },[input]);
   
  
    
  return (
        <Container maxWidth="md">
          <div className={classes.formWrapper}>

            <Formik
              initialValues={{address:'',pincode:''}}
              onSubmit={values => {
                console.log(values);
              }}
            >
            {props => {
              const {
                values,
                handleChange,
                setFieldValue
              } = props;
              return (
                <Form>
                  <Grid container spacing={2}>
                      <Grid item xs={12}>
                        <Typography>
                          Your details
                        </Typography>
                      </Grid>

                      <Grid item xs={6}>
                        <Textfield
                          name="address"
                          label="Address"
                          value = {values.address}
                        />
                      </Grid>

                      <Grid item xs={6}>
                        <Textfield
                          name="pincode"
                          label="Pincode"
                          value={values.pincode}
                        />
                  {setInput(values.address)}
                      </Grid>
                      <Grid item xs={12}>
                        <Button>
                          Submit Form
                        </Button>
                      </Grid>
                  </Grid>
                </Form>
              )}}
            </Formik>

          </div>
        </Container>
  );
};
about 4 years ago · Santiago Trujillo
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