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

161
Visualizações
How to set default value and value in react js Text Field at same time

I have react Text Field with defaultValue.In defaultValue, I set the value using props. But I need to set onchage() event to that text field, So then I can update my TextField value. But I can't set them. When I trying to set a value, my default value not showing.

          const [openingHours, setOpeningHours] = useState({
               monday: '',
           });


        const mondayHandler = (event) => {
                  const temp = {...openingHours};
                   temp.monday = event.target.value;
                 setOpeningHours(temp);
             };

         <TextField
                type="time"
                defaultValue={props.times[0].time1 !== undefined ? props.times[0].time1 : '00:00'}
                className={classes.textField}
                InputLabelProps={{
                    shrink: true,
                    color: 'blue',
                }}
                InputProps={{
                    classes: {
                        underline: classes.underline,
                        root: classes.timeRoot,
                    },
                }}
            />

How to set Onchange event in to this text field..?

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

0

Disclaimer: I am guessing that you are using material-ui's TextField component. If I am wrong, this answer might not hold water.

By using value and onChange to control the state of the TextField you are creating what some people call a "controlled input". The defaultValue prop is meant to be used with uncontrolled inputs, not controlled. Instead, you can set a default value by supplying the correct initial value to useState.

What I'm suggesting would look something like:

        const [openingHours, setOpeningHours] = useState({
            monday: '00:00',
        });


        const mondayHandler = (event) => {
            const temp = {...openingHours};
            temp.monday = event.target.value;
            setOpeningHours(temp);
        };

         <TextField
             type="time"
             value={openingHours.monday}
             onChange={mondayHandler}
             ...
         />

You will have to verify that 00:00 is a valid value for a TextField with type="time". You might need to supply a Javascript date object instead.

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