์ ๋ ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์ ์์๋ก ๊ตฌํํ๊ฒ ์ต๋๋ค!
์ฐ์ ์ต์ข ์ฝ๋์ ๋๋ค. ๐
import * as React from "react";
export default function PasswordChange() {
// input ์ปดํฌ๋ํธ์ id ์ ์ธ
// ํ์ฌ ๋น๋ฐ๋ฒํธ
const CURRENT_PASSWORD = "currentPassword";
// ์ ๋น๋ฐ๋ฒํธ
const NEW_PASSWORD = "newPassword";
// ์ ๋น๋ฐ๋ฒํธ ํ์ธ
const NEW_PASSWORD_CONFIRM = "newPasswordConfirm";
// input ์ปดํฌ๋ํธ์ ๋ง๋ id๋ณ์ ๋ค
const [inputs, setInputs] = React.useState({
currentPassword: "",
newPassword: "",
newPasswordConfirm: ""
});
const { currentPassword, newPassword, newPasswordConfirm } = inputs; // inputs์์ ๋น๊ตฌ์กฐํ ํ ๋น์ ํตํด ๊ฐ ์ถ์ถ
// ๋น๋ฐ๋ฒํธ ์
๋ ฅ
const handleInput = (event) => {
const { id, value } = event.target;
setInputs({
...inputs, // ๊ธฐ์กด์ input ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ ๋ค
[id]: value // id ํค๋ฅผ ๊ฐ์ง input๋ง์ value๋ก ์ค์
});
};
return (
<div>
<div>
<input
id={CURRENT_PASSWORD}
placeholder="๋น๋ฐ๋ฒํธ"
value={currentPassword}
onChange={handleInput}
/>
</div>
<div>
<input
id={NEW_PASSWORD}
placeholder="์ ๋น๋ฐ๋ฒํธ"
value={newPassword}
onChange={handleInput}
/>
</div>
<div>
<input
id={NEW_PASSWORD_CONFIRM}
placeholder="์ ๋น๋ฐ๋ฒํธ ํ์ธ"
value={newPasswordConfirm}
onChange={handleInput}
/>
</div>
</div>
);
}
ํ์ํ ์ปดํฌ๋ํธ ๊ตฌํ
PasswordChange.js
export default function PasswordChange() {
return (
<div>
<div>
<input placeholder="๋น๋ฐ๋ฒํธ" />
</div>
<div>
<input placeholder="์ ๋น๋ฐ๋ฒํธ" />
</div>
<div>
<input placeholder="์ ๋น๋ฐ๋ฒํธ ํ์ธ" />
</div>
</div>
);
}
input ์ปดํฌ๋ํธ์ id ์ ์ธ
๊ฐ input์ id๋ฅผ ์ ์ธํด์ค๋๋ค.
PasswordChange.js
export default function PasswordChange() {
// input ์ปดํฌ๋ํธ์ id ์ ์ธ
// ํ์ฌ ๋น๋ฐ๋ฒํธ
const CURRENT_PASSWORD = "currentPassword";
// ์ ๋น๋ฐ๋ฒํธ
const NEW_PASSWORD = "newPassword";
// ์ ๋น๋ฐ๋ฒํธ ํ์ธ
const NEW_PASSWORD_CONFIRM = "newPasswordConfirm";
return (
<div>
<div>
<input id={CURRENT_PASSWORD} placeholder="๋น๋ฐ๋ฒํธ" />
</div>
<div>
<input id={NEW_PASSWORD} placeholder="์ ๋น๋ฐ๋ฒํธ" />
</div>
<div>
<input id={NEW_PASSWORD_CONFIRM} placeholder="์ ๋น๋ฐ๋ฒํธ ํ์ธ" />
</div>
</div>
);
}
์ ์ด ํจ์ ๊ตฌํ
์ฐธ๊ณ ๋ฅผ ์ํด ์ ๋ ฅ event.target๋ฅผ ์ฝ์๋ก ์ฐ์ด id ์ value ๊ฐ์ ํ์ธํด๋ณด๊ฒ ์ต๋๋ค.
๐ handleInput() ํจ์์์ const { id, value } = event.target; ๋ก id, value๋ฅผ ์ถ์ถํด๋ ๋๋ค.
import * as React from "react";
export default function PasswordChange() {
// input ์ปดํฌ๋ํธ์ id ์ ์ธ
// ํ์ฌ ๋น๋ฐ๋ฒํธ
const CURRENT_PASSWORD = "currentPassword";
// ์ ๋น๋ฐ๋ฒํธ
const NEW_PASSWORD = "newPassword";
// ์ ๋น๋ฐ๋ฒํธ ํ์ธ
const NEW_PASSWORD_CONFIRM = "newPasswordConfirm";
// input ์ปดํฌ๋ํธ์ ๋ง๋ id๋ค
const [inputs, setInputs] = React.useState({
currentPassword: "",
newPassword: "",
newPasswordConfirm: ""
});
const { currentPassword, newPassword, newPasswordConfirm } = inputs; // inputs์์ ๋น๊ตฌ์กฐํ ํ ๋น์ ํตํด ๊ฐ ์ถ์ถ
// ๋น๋ฐ๋ฒํธ ์
๋ ฅ
const handleInput = (event) => {
const { id, value } = event.target;
setInputs({
...inputs, // ๊ธฐ์กด์ input ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ ๋ค
[id]: value // id ํค๋ฅผ ๊ฐ์ง input๋ง์ value๋ก ์ค์
});
};
return (
<div>
<div>
<input
id={CURRENT_PASSWORD}
placeholder="๋น๋ฐ๋ฒํธ"
value={currentPassword}
onChange={handleInput}
/>
</div>
<div>
<input
id={NEW_PASSWORD}
placeholder="์ ๋น๋ฐ๋ฒํธ"
value={newPassword}
onChange={handleInput}
/>
</div>
<div>
<input
id={NEW_PASSWORD_CONFIRM}
placeholder="์ ๋น๋ฐ๋ฒํธ ํ์ธ"
value={newPasswordConfirm}
onChange={handleInput}
/>
</div>
</div>
);
}
๋นํจ์จ์ ์ผ ์ ์๋ ์ฝ๋...! ๐ค
๊ฐ value์ ๋ง๋ ํจ์๋ฅผ ์ผ์ผ์ด ์์ฑํด์ฃผ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋ฒ๊ฑฐ๋ก์ธ ์ ์์ต๋๋ค. ๐๐
import * as React from "react";
export default function App() {
// ๋น๋ฐ๋ฒํธ
const [currentPassword, setCurrentPassword] = React.useState("");
// ์ ๋น๋ฐ๋ฒํธ
const [newPassword, setNewPassword] = React.useState("");
// ์ ๋น๋ฐ๋ฒํธ ํ์ธ
const [newPasswordConfirm, setNewPasswordConfirm] = React.useState("");
// ๋น๋ฐ๋ฒํธ ์
๋ ฅ ์ ์ด ํจ์
const handleCurrentPassword = (event) => {
setCurrentPassword(event.target.value);
};
// ์ ๋น๋ฐ๋ฒํธ ์
๋ ฅ ์ ์ด ํจ์
const handleNewPassword = (event) => {
setNewPassword(event.target.value);
};
// ์ ๋น๋ฐ๋ฒํธ ํ์ธ ์
๋ ฅ ์ ์ด ํจ์
const handleNewPasswordConfirm = (event) => {
setNewPasswordConfirm(event.target.value);
};
return (
<div>
<div>
<input
placeholder="๋น๋ฐ๋ฒํธ"
value={currentPassword}
onChange={handleCurrentPassword}
/>
</div>
<div>
<input
placeholder="์ ๋น๋ฐ๋ฒํธ"
value={newPassword}
onChange={handleNewPassword}
/>
</div>
<div>
<input
placeholder="์ ๋น๋ฐ๋ฒํธ ํ์ธ"
value={newPasswordConfirm}
onChange={handleNewPasswordConfirm}
/>
</div>
</div>
);
}
๊ตฟ!