Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

controlled input

import { useState } from "react";
function MyControlledInput({}) {
  const [value, setValue] = useState("");

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

  const restInputValue = () => {
    setValue("");
  };

  return (
    <>
      <div>Input value: {value}</div>
      <input value={value} onChange={onChange} />
      {/* This one will not reset the input value */}
      <input onChange={onChange} />
      <button onClick={restInputValue}>rest input value</button>
    </>
  );
}

export default MyControlledInput;
Source by dmitripavlutin.com #
 
PREVIOUS NEXT
Tagged: #controlled #input
ADD COMMENT
Topic
Name
5+9 =