getElemnetById
, querySelector
์ ๊ฐ์ DOM Selector ํจ์๋ฅผ ์ฌ์ฉํ์ฌ DOM์ ์ ํref
์ฌ์ฉref
๋ฅผ ์ฌ์ฉํ ๋ useRef
Hook ํจ์ ์ฌ์ฉuseRef
๋ฅผ ์ฌ์ฉํด์ Ref ๊ฐ์ฒด ์์ฑref
๊ฐ์ผ๋ก ์ค์ onReset
ํจ์์์ input์ ํฌ์ปค์คํ๋ focus()
DOM API ํธ์ถ// InputSample.js
import React, { useRef, useState } from "react";
function InputSample() {
const [inputs, setInputs] = useState({
name: "",
nickname: "",
});
const nameInput = useRef();
const { name, nickname } = inputs;
const onChange = (e) => {
const { value, name } = e.target;
setInputs({
...inputs,
[name]: value,
});
};
const onReset = () => {
setInputs({
name: "",
nickname: "",
});
nameInput.current.focus();
};
return (
<div>
<input
name="name"
placeholder="์ด๋ฆ"
onChange={onChange}
value={name}
ref={nameInput}
/>
<input
name="nickname"
placeholder="๋๋ค์"
onChange={onChange}
value={nickname}
/>
<button onClick={onReset}>์ด๊ธฐํ</button>
<div>
<b>๊ฐ: </b>
{name} ({nickname})
</div>
</div>
);
}
export default InputSample;
// UserList.js
import React from "react";
function UserList() {
const users = [
{
id: 1,
username: "velopert",
email: "public.velopert@gmail.com",
},
{
id: 2,
username: "tester",
email: "tester@example.com",
},
{
id: 3,
username: "liz",
email: "liz@example.com",
},
];
return (
<div>
<div>
<b>{users[0].username}</b> <span>({users[0].email})</span>
</div>
<div>
<b>{users[1].username}</b> <span>({users[1].email})</span>
</div>
<div>
<b>{users[2].username}</b> <span>({users[2].email})</span>
</div>
</div>
);
}
export default UserList;
// UserList.js
import React from "react";
function User({ user }) {
return (
<div>
<b>{user.username}</b> <span>({user.email})</span>
</div>
);
}
function UserList() {
const users = [
{
id: 1,
username: "velopert",
email: "public.velopert@gmail.com",
},
{
id: 2,
username: "tester",
email: "tester@example.com",
},
{
id: 3,
username: "liz",
email: "liz@example.com",
},
];
return (
<div>
<User user={users[0]} />
<User user={users[1]} />
<User user={users[2]} />
</div>
);
}
export default UserList;
map()
์ฌ์ฉmap()
: ๋ฐฐ์ด ์์ ์๋ ๊ฐ ์์๋ฅผ ๋ณํํ์ฌ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํmap()
์ ์ฌ์ฉํ์ฌ ์ผ๋ฐ ๋ฐ์ดํฐ ๋ฐฐ์ด์ ๋ฆฌ์กํธ ์๋ฆฌ๋จผํธ๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด๋ก ๋ณํ// UserList.js
import React from "react";
function User({ user }) {
return (
<div>
<b>{user.username}</b> <span>({user.email})</span>
</div>
);
}
function UserList() {
const users = [
{
id: 1,
username: "velopert",
email: "public.velopert@gmail.com",
},
{
id: 2,
username: "tester",
email: "tester@example.com",
},
{
id: 3,
username: "liz",
email: "liz@example.com",
},
];
return (
<div>
{users.map((user) => (
<User user={user} />
))}
</div>
);
}
export default UserList;
key
๋ผ๋ props๋ฅผ ์ค์ ํ์ง ์์ผ๋ฉด ์๋ฌ ๋ฐ์key
๊ฐ์ ๊ฐ ์์๋ค๋ง๋ค ๊ฐ์ง๊ณ ์๋ ๊ณ ์ ๊ฐ์ผ๋ก ์ค์ : ์ง๊ธ์ id
๊ฐ ๊ณ ์ ๊ฐreturn (
<div>
{users.map((user) => (
<User user={user} key={user.id} />
))}
</div>
);
map()
์ ์ฝ๋ฐฑํจ์์ ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ index
๋ฅผ key
๋ก ์ฌ์ฉํ ์ ์๋ค.return (
<div>
{users.map((user, index) => (
<User user={user} key={index} />
))}
</div>
);
useRef
Hook์ ์ปดํฌ๋ํธ ์์์ ์กฐํ ๋ฐ ์์ ํ ์ ์๋ ๋ณ์๋ฅผ ๊ด๋ฆฌ ํ ์ ์๋ค.
=> useRef
๋ก ๊ด๋ฆฌํ๋ ๋ณ์๋ ์ค์ ํ ๋ฐ๋ก ์กฐํ ๊ฐ๋ฅ
(๋ฆฌ์กํธ ์ปดํฌ๋ํธ์์ ์ํ๋ ์ํ๋ฅผ ๋ฐ๊พธ๋ ํจ์๋ฅผ ํธ์ถ ํ, ๋ ๋๋งํ ๋ค์ ์
๋ฐ์ดํธ ๋ ์ํ๋ฅผ ์กฐํํ ์ ์๋ค.)
useRef
๋ก ๊ด๋ฆฌํ๋ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ๊ด๋ฆฌ ํ ์ ์๋ค.
setTimeout
, setInterval
์ ํตํด ์์ฑ๋ id
import { useRef } from "react";
const refContariner = useRef(์ด๊ธฐ๊ฐ);
refContariner.current += 1; // ๊ฐ ์์
refContariner.current; // ๊ฐ ๋ถ๋ฌ์ค๊ธฐ
useRef
์ ์ ์ฅ๋ ๊ฐ์ ์ ๊ทผํ๊ธฐ ์ํด .current
๋ฅผ ์ฌ์ฉ
=> useRef
๋ .current
ํ๋กํผํฐ์ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๊ฐ์ ๋ด๊ณ ์๋ ์์!
useRef
๋ ์ด๋ค ๊ฐ๋ณ๊ฐ์ ์ ์งํ๋๋ฐ ์ ์ฉ
=> useRef
๋ก ๊ด๋ฆฌํ๋ ๋ณ์๋ ๊ฐ์ด ๋ฐ๋์ด๋ ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง ๋์ง ์๋๋ค.
(useState
๋ ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋๋ง๋ค ๋ฆฌ๋ ๋๋ง์ด ์ผ์ด๋๋ค.)
์์ ์ฝ๋
// useState ๋ถ๋ถ ์ฃผ์์ฒ๋ฆฌ ํ๋ฉด์
// const, useRef, useState ๋น๊ตํด๋ณด๊ธฐ
import { useRef, useState } from "react";
export default function Example() {
let count1 = 0;
const count2 = useRef(0);
const [count3, setCount3] = useState(0);
const Check = () => {
count1 += 1;
console.log(`count1 : ${count1}`);
count2.current += 1;
console.log(`count2 : ${count2}`);
setCount3(count3 + 1);
};
return (
<>
<h2>์์ </h2>
<p>count1 : {count1}</p>
<p>count2 : {count2.current}</p>
<p>count3 : {count3}</p>
<button onClick={Check}>check</button>
</>
);
}
const
์ useRef
const
๊ฐ์ด ๋ณํ๋ ๊ฒ, useRef
๋ object๋ฅผ ๋ฆฌํดํ๋ ๊ฒ์ ํ์ธconst
์ useRef
ํ์ธ์ ์ํ ๋ฆฌ๋ ๋๋งconst
๊ฐ์ 0์ผ๋ก ์ด๊ธฐํ, useRef
๊ฐ์ ๋ฆฌ๋ ๋๋ง ์ ์ ์ฅ๋์๋ ๊ฐ์ด ๋ํ๋๊ฒ ๋๋ค.const
, useRef
๊ทธ๋ฆฌ๊ณ useState
useState
๋ ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋๋ง๋ค ๋ฆฌ๋ ๋๋ง์ด ์ผ์ด๋๋ค.useState
์ผ๋ก ์ธํด ๋ฆฌ๋ ๋๋ง์ด ์ผ์ด๋๋ฉด์ useRef
๊ฐ๋ ํจ๊ป ๋ณ๊ฒฝ๋์ง๋ง const
๊ฐ์ ์ด๊ธฐํ๊ฐ ๋์ด 1๋ง ์ถ๋ ฅ๋๋ค.์ถ๊ฐ์ ์ผ๋ก ๊ณต๋ถํ ๋ถ๋ถ
- useState์ ๋น๋๊ธฐ์ ๊ฐ ํ ๋น
์ฐธ๊ณ