1

I'm triyng to remove an object property using spread syntax rendering a React component. I wonder if there is a way to achieve without adding to much extra code. I'm using {reset,...inputName}

I have a custom hook (useField) for every input in my form. useField also has a reset function for my reset button. But I want to remove the reset property only for the inputs element.

custom hook useField

export const useField = (type) => {

  const [value, setValue] = useState('')

  const onChange = (event) => {
    setValue(event.target.value)
  }

  const reset = () => {
    setValue('')
  }

  return {
    type,
    value,
    onChange,
    reset
  }
}

react form

const MyForm = (props) => {

  const content = useField('text')
  const author = useField('text')
  const info = useField('text')
 
  const handleClick = () => {
    content.reset()
    author.reset()
    info.reset()
  }

  return (
    <div>
      <h2>create a new anecdote</h2>
      <form onSubmit={handleSubmit}>
        <div>
          content
          <input {reset,...content} />
        </div>
        <div>
          author
          <input {reset,...author} />
        </div>
        <div>
          url for more info
          <input {reset,...info} />
        </div>
        <button>create</button>
        <input type="reset" value='reset' onClick={handleClick} />
      </form>
    </div>
  )

}
4
  • You can't delete properties in a spread like that but you can make a function that does it and spread the result for less clutter? Commented Mar 14, 2022 at 19:22
  • Suppose we have an object like so: const obj = {'a': 10, 'b': 20, 'c': 30, 'd': 40}; and we need to 'remove' prop 'a' using ... spread - it can be done like this: const {a, ...rest} = obj; const newObj = {...rest};. Now, newObj will not have the prop a. If you replace const with let, then instead of newObj, you can say obj itself. So, effectively, 'a' is removed from obj. Commented Mar 14, 2022 at 19:25
  • 1
    Please trying changing: const content = useField('text') to const {reset: resetContent, ...content} = useField('text'). I am hoping that reset will no longer be within content. Then, you may use resetContent in your handleClick while using content for JSX. Commented Mar 14, 2022 at 19:30
  • 1
    @jsN00b great solution, thanks! It's exactly what i was looking for, avoid to lose much of the benefit provided by custom hooks and spread syntax! Commented Mar 14, 2022 at 19:46

1 Answer 1

1

For future reference, what may work for OP are changes similar to below:

  const { reset: resetContent, ...content} = useField('text')
  const { reset: resetAuthor, ...author} = useField('text')
  const { rest: resetInfo, ...info} = useField('text')
 
  const handleClick = () => {
    resetContent();
    resetAuthor();
    resetInfo();
  };
.
.
.
  <div>
   content
   <input {...content} />
  </div>
.
.
.

Explanation

  • the object returned from useField is destructured
  • the reset prop is separated and renamed as resetContent (or resetAuthor, resetInfo, as required)
  • the rest of the props go into the content variable (or author, info variables, as required)
  • when rendering in the JSX, the content is used

Thus, effectively the reset prop from useField was 'removed' (technically, it was just separated, though) in the new content object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.