Skip to content

Commit

Permalink
a form with useRef to managing inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jessilyneh committed Mar 2, 2022
1 parent 88157e0 commit eafd81b
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import React, {useReducer} from "react";
import React, {useRef} from "react";
import ReactDOM from "react-dom";
import "./index.css";

const initialState = {
message: "hello"
};

// send actions
function reducer(state, action) {
// eslint-disable-next-line default-case
switch (action.type) {
case "yell":
return {
message: `HEY, my default message was ${state.message}`
};
case "yup":
return {
message: `YO, this is my default msg: ${state.message}`
};
}
}
export default function App() {
const [state, dispatch] = useReducer(reducer, initialState);
const sound = useRef();
const date = useRef();

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 = "";
};

return (
<>
<p>Message:{state.message}</p>
<button onClick={() => dispatch({ type: "yell" })}>yell</button>
<button onClick={() => dispatch({ type: "yup" })}>yup</button>
</>
<form onSubmit={submit}>
<input ref={sound} type="text" placeholder="name..." />

<input ref={date} type="date" />

<button>Submit</button>
</form>
);
}


ReactDOM.render(
<React.StrictMode>
<App />
Expand Down

0 comments on commit eafd81b

Please sign in to comment.