score:0

type TPerson = {
  name: string;
  car: string;
};

const defaultPerson: TPerson = { name: '', car: '' }

export default function App() {
  // use defaultValue
  const [person, setPerson] = React.useState<TPerson>(defaultPerson);

  const handleClear = React.useCallback(() => {
    // set this to defaultPerson
    setPerson(defaultPerson);
  }, []);

  const { name, car } = person;

  return (
    <div className="App">
      <h1>
        {person.name} drives a {person.car}
      </h1>
      <button onClick={handleClear}>clear</button>
    </div>
  );
}


Related Query

More Query from same tag