Skip to content

Commit

Permalink
a controlled component with useState
Browse files Browse the repository at this point in the history
  • Loading branch information
jessilyneh committed Mar 3, 2022
1 parent 414cccd commit 103f5b8
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import React, {useRef} from "react";
import React, {useState} from "react";
import ReactDOM from "react-dom";
import "./index.css";

export default function App() {
const sound = useRef();
const date = useRef();
const [name, setName] = useState("")
const [birth, setBirth]= useState("")

//controlled components are those in which
// form data is handled by the component's state
const submit = (e) => {
e.preventDefault();
const soundValue = sound.current.value;
const dateValue = date.current.value;
alert(`${soundValue} born in ${dateValue}`);
sound.current.value = "";
date.current.value = "";
alert(`${name} was born in ${birth}`);
setName("")
setBirth("")
};

return (
<form onSubmit={submit}>
<input ref={sound} type="text" placeholder="name..." />
<input value={name}
type="text"
placeholder="name..."
onChange={(e)=> setName(e.target.value)}
/>

<input ref={date} type="date" />
<input value={birth}
type="date"
onChange={(e)=> setBirth(e.target.value)}
/>

<button>Submit</button>
</form>
Expand Down

0 comments on commit 103f5b8

Please sign in to comment.