0

How to clear empty form input state in React before sending to server?

For example, in the form below, if user.name is not filled in then the object sent to the server is:

{ age: "19" }

Note: I can of course delete the empty properties manually in submitHandler, but is there any other way that doesn't require me to manually delete?

const UserAdd = () => {
  const [user, setUser] = useState({
    name: "",
    age: ""
  });

  const inputChangeHandler = (e) => {
    setUser({ ...user, [e.target.name]: e.target.value })
  }

  const submitHandler = (e) => {
    e.preventDefault();
    
    // Start Solution
    const userWithoutEmptyProperties = {};
    for(const key in user) {
      if(user[key]) {
        userWithoutEmptyProperties[key] = user[key];
      }
    }
    // End Solution

    // send "userWithoutEmptyProperties" to server 
  }

  return (
    <form onSubmit={submitHandler} >
      <input type="text" name="name" onChange={inputChangeHandler} />
      <input type="text" name="age" onChange={inputChangeHandler} />
      <button type="submit">Add</button>
    </form>
  )
}
10
  • What's wrong with what you have? It's simple, clear, doesn't create a lot of unnecessary intermediate objects, make a lot of unnecessary function calls, ... Commented Oct 22, 2021 at 9:40
  • @T.J.Crowder I feel this is too wordy. I want to get the same behavior when I initialize state with an empty object, the problem with this React is giving me a warning. Commented Oct 22, 2021 at 9:43
  • Okay. The linked question's answers give you various alternatives, particularly this one. Commented Oct 22, 2021 at 9:47
  • Nothing different from the solution I wrote in submitHandler, I want to get another way, unfortunately my question is considered a duplicate, even though I already explained that I want to use another way, which should not be a duplicate. Commented Oct 22, 2021 at 9:53
  • 1
    "I can of course delete the empty properties manually in submitHandler, but is there any other way that doesn't require me to manually delete?". Thank you i really appreciate your answer. I don't seem to be able to convey my meaning properly because of my language limitations. Commented Oct 22, 2021 at 10:01

2 Answers 2

0
Object.keys(user).forEach((item) => !user[item] && delete user[item);
Sign up to request clarification or add additional context in comments.

1 Comment

That modifies the original object, which you must not do when objects stored in React state. Also, the OP said "I can of course delete the empty properties manually in submitHandler..." which is exactly what this does. They're looking for a *different solution.
0

You can use one-liner:

let o = Object.fromEntries(Object.entries(obj).filter(([_, v]) => v));

More about this: Remove blank attributes from an Object in Javascript

1 Comment

Please vote to close as duplicate when the answers to a different question answer a question, rather than posting an answer. More in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.