Thursday, 22 September 2022

What are the consequences of mutating event.target.value (in React)?

Let's say we are building custom input component.

In this component, as an example let's say we want to change the value from a string to a number

const CustomInputComponent = ({ onChange, ...rest }) => {
  const onChangeHandler = (event)=>{
    // What are the consequences of doing this?
    event.target.value = parseInt(event.target.value, 10);
    onChange(event);
  }
  return <input type="text" onChange={onChangeHandler} />
}

What are the consequences of directly mutating event.target.value like this?



from What are the consequences of mutating event.target.value (in React)?

No comments:

Post a Comment