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

295
Visualizações
How to empty a text box by using a button in react?

I have a text box to input numbers, and after pressing the button, I want to clear the text box value. I have no idea how to do it, and can anybody help me?

Table

Example: When I press the add button in the Chairs row, 10 should be cleared.

<CRow>
  <CCol className="col-sm-6">
    <CFormInput
      id="quantity"
      type="number"
      name="quantity"
      max={item.available}
      min={1}
      onChange={(e) => {
        resetErrors()
        quantity[index] = e.target.value
      }}
    />
  </CCol>
  <CCol>
    <CButton
      color="success"
      size="sm"
      onClick={() => {
        resetErrors()
        handleSave(item._id, index)
      }}
    >
      <CIcon icon={cilPlus} className="me-2" />
      Add
    </CButton>
  </CCol>
</CRow>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should define a state for the quantity and then update that state whenever the user enters a new value to that new value. Or when the Clear button is pressed update it to 0.

You could of course also update to an empty string if you want. You would then just need to be careful with the types as + means string concatenation if a string is involved, not addition. So make sure your program handles that correctly.

function App() {
  const [quantity, setQuantity] = React.useState(0);

  return (
    <React.Fragment>
      <input value={quantity} type="number" onChange={e => setQuantity(e.target.valueAsNumber)} />
      <button onClick={() => setQuantity(0)}>Clear</button>
    </React.Fragment>
  );
}

ReactDOM.render(<App/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Also it seems you are using an array as for all quantities which is perfectly fine, just make sure that you create a new array when you update a quantity as state should be treated as immutable. Additionally, in React and re-render is only triggered when the reference of the array changes as only a shallow comparison (NOT a deep comparison) of arrays is done. You could do that e.g. like this:

This thread might also be helpful in that regard.

// original
const quantities = [3, 12, 24];
// index to update the array on
const index = 1;
// create a new array with the quantity at index increased by 1
const newQuantities = [...quantities.slice(0, index), quantities[index] + 1, ...quantities.slice(index + 1)];

// arrays are actually two different arrays
console.log(quantities);
console.log(newQuantities);
// references are not the same
console.log(quantities === newQuantities);

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