skip to Main Content

Using controlled input to set the value of my input area, but it occurs that when i use backspace on the input field it doesn’t delete the last character in the object where i am saving the input values.

I have don’t know what to try because i’m entirely new to react.

2

Answers


  1. When using controlled input in React, you need to include two arguments (or attributes) for the input element: value and onChange event to handle the value change event.

    To define a state for the input element:

    const [email, setEmail] = useState('')
    

    and setting the attributes:

     <input
         type="email"
         name="email"
         value={email}
         onChange={event => setEmail(event.target.value)}
     />
    

    If hitting the return key doesn’t delete, it’s because the onChange event is missing.

    Login or Signup to reply.
  2. You must work with inputs using useState.

    If you’re starting out with React and don’t know exactly what to do, you should have your code something like this:

    import React, { useState } from 'react';
    
    function MyComponent() {
      const [inputValue, setInputValue] = useState('');
    
      const handleInputChange = (event) => {
        setInputValue(event.target.value);
      };
    
      return (
        <input
          type="text"
          value={inputValue}
          onChange={handleInputChange}
        />
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search