React 의 이벤트 처리 방식은 DOM 의 이벤트 처리 방식과 유사합니다.
단, 몇 가지 문법 차이가 있습니다.
기존 DOM의 이벤트처리 방식과 유사하지만 몇 가지 문법 차이가 있습니다.
// HTML 이벤트 처리 방식
<button onclick="handleEvent()">Event</button>
// React 이벤트 처리 방식
<button onClick={handleEvent}>Event</button>
import React, { useState } from "react";
function App() {
const [name, setName] = useState("");
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setName(e.target.value);
// console.log(e.target.value);
};
return (
<>
<input type="text" value={name} onChange={handleChange}></input>
<h1>{name}</h1>
</>
);
}
export default App;
// atoms.tsx
import { atom } from "recoil";
export const nameState = atom({
key: "name",
default: "",
});
// App.tsx
import React from "react";
import { useRecoilState } from "recoil";
import { nameState } from "./atoms";
function App() {
const [name, setName] = useRecoilState(nameState);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setName(e.target.value);
// console.log(e.target.value);
};
return (
<>
<input type="text" value={name} onChange={handleChange}></input>
<h1>{name}</h1>
</>
);
}
export default App;
import React from "react";
import { useRecoilState } from "recoil";
import { nameState } from "./atoms";
function App() {
const [name, setName] = useRecoilState(nameState);
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setName(event.target.value);
// console.log(event.target.value);
};
const handleClick = () => {
alert(name);
};
return (
<>
<input type="text" value={name} onChange={handleChange}></input>
<button onClick={handleClick}>Button</button>
<h1>{name}</h1>
</>
);
}
export default App;
import React from "react";
import { useRecoilState } from "recoil";
import { nameState } from "./atoms";
function App() {
const [name, setName] = useRecoilState(nameState);
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setName(event.target.value);
// console.log(e.target.value);
};
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert(name);
};
return (
<>
<form onSubmit={handleSubmit}>
<input type="text" value={name} onChange={handleChange}></input>
<button type="submit">Button</button>
</form>
<h1>{name}</h1>
</>
);
}
export default App;
import React, { useState } from "react";
function App() {
const [choice, setChoice] = useState("apple");
const fruits = ["apple", "orange", "pinapple", "strawberry", "grage"];
const options = fruits.map((fruit) => {
return <option value={fruit}>{fruit}</option>;
});
const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setChoice(event.target.value);
};
return (
<>
<select onChange={handleChange}>{options}</select>
<h3>You choose "{choice}"</h3>
</>
);
}
export default App;
// atoms.tsx
import { atom } from "recoil";
export const choiceState = atom({
key: "choice",
default: "apple",
});
// App.tsx
import React from "react";
import { useRecoilState } from "recoil";
import { choiceState } from "./atoms";
function App() {
const [choice, setChoice] = useRecoilState(choiceState);
const fruits = ["apple", "orange", "pinapple", "strawberry", "grage"];
const options = fruits.map((fruit) => {
return <option value={fruit}>{fruit}</option>;
});
const handleChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setChoice(event.target.value);
};
return (
<>
<select onChange={handleChange}>{options}</select>
<h3>You choose "{choice}"</h3>
</>
);
}
export default App;
코드스테이츠 프론트엔드 부트캠프