2023. 6. 25. 17:26ใStudy_Develop/ํฌ์ค์ฝ X ์ฝ๋ฉ์จ ๋ถํธ์บ ํ
9์ฃผ์ฐจ๋ ๋ ๊ฐ์๋ค ๋จ๋ ๋๋ฌ๋ค.
์๋๋ ๊ทธ๋ฌ์ง๋ง
์์ฆ๋ฐ๋ผ.. ๋์ฑ์ด
๊ณต๋ถํ๊ธฐ ์ซ๋ค.
ใ ใ ์ด์ฉ์ง..
ํด์ผํ ๊ฒ ๋๋ฌด ๋ง์์
์๋ฌด๊ฒ๋ ์ํ๋ ์ค ใ
ํ์ง๋ง.. ์ค๋ ๋๊ฐ ์์ ์
๊ผญ ๋ณต์ต์ ํด์ผ๋ง ๋ด์ผ์์ ์ ๋ค์ ์ ์๋ค
๊ทธ๋ฐ์๋ฏธ์์ ์ค๋ ๋ฐฐ์ด ๋ด์ฉ
๊พธ์ญ ๊พธ์ญ ๋ณต์ต์ ํด๋ณธ๋ค..
map()
์ผ๋ฐ for ๋ฐ๋ณต๋ฌธ
- map()์ ์ธ์๋ก ๋๊ฒจ์ง๋ callback ํจ์๋ฅผ ์คํํ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๊ณ
์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค ๋ ์ฌ์ฉ
- map()ํจ์๋ฅผ ํ์์ ๋ฐ๋ผ
๋ฐ๋ณต๋ฌธ์ฒ๋ผ ์ฌ์ฉํ ์๋ ์์
๋ฌธ๋ฒ
arr.map( callbackFunction, [thisArg] )
callbackFunction
: ์๋ก์ด ๋ฐฐ์ด์ ์์๋ฅผ ์์ฑํ๋ ํจ์๋ก,
currentValue, index, array 3๊ฐ์ ์ธ์๋ฅผ ๊ฐ์ง ์ ์๋ค.
[this.Arg]
: ์๋ต ๊ฐ๋ฅํ๊ณ , callbackFunction์์ ์ฌ์ฉํ this๊ฐ์ฒด
let list = ['a', 'b', 'c', 'd', 'e'];
let items = list.map((txt, id, arr)=>{
console.log("tx: ", txt);
console.log("tx: ", txt);
console.log("tx: ", txt);
return txt + id;
})
- txt : list๋ฅผ ์์๋๋ก ๋๋ฉด์ ๋์ค๋ ๊ฐ
- id : ๋ฐฉ๊ธ ๋์จ ๊ฐ(txt)์ ์ธ๋ฑ์ค
- arr : ํ์ฌ ๋ฐ๋ณต์ ๋๊ณ ์๋ ๋ฐฐ์ด
- items : "return txt + id;"๋ก ๋ง๋ค์ด์ง ๋ฐฐ์ด
Component์ map() ์ ์ฉ
function Repeat() {
const list = ["a", "b", "c", "d", "e"];
return (
<>
<div>
{list.map((value) => {
return <div>{value}</div>;
})}
</div>
</>
);
}
input ํ๊ทธ๋ก ์๋ก์ด ์ํ๋ฒณ ์ถ๊ฐํ๊ธฐ
const addAlpha = () => {
//concat() ์ธ์๋ก ์ฃผ์ด์ง ๊ฐ์ ๊ธฐ์กด ๋ฐฐ์ด์ ํฉ์น๊ธฐ
const newAlpha = list.concat({ id: list.length + 1, alpha: inputAlpha });
setList(newAlpha);
setInputAlpha("");
};
return (
<>
<input
type="text"
value={inputAlpha}
onChange={(e) => setInputAlpha(e.target.value)}
/>
<button onClick={addAlpha}>์ถ๊ฐ</button>
<ol>
{list.map((value) => {
console.log(value);
return (
<li key={value.id}}>
{value.alpha}
</li>
);
})}
</ol>
</>
);
โ concat()
: ์ธ์๋ก ์ฃผ์ด์ง ๋ฐฐ์ด์ด๋ ๊ฐ๋ค์
๊ธฐ์กด ๋ฐฐ์ด์ ํฉ์ณ์ ์ ๋ฐฐ์ด์ ๋ฐํํ๋ค.
: concat์ this๋ ์ธ์๋ก ๋๊ฒจ์ง ๋ฐฐ์ด์ ๋ด์ฉ์ ๋ฐ๊พธ์ง ์๊ณ ,
๋์ ์ฃผ์ด์ง ๋ฐฐ์ด์ ํฉ์น๋ค ๊ทธ ์์ ์ฌ๋ณธ์ ๋ฐํํ๋ค.
concat ์์ >
//๋ฐฐ์ด ๋๊ฐ ์ด์ด ๋ถ์ด๊ธฐ
const alpha = ['a', 'b', 'c'];
const numeric = [1, 2, 3];
alpha.concat(numeric);
// ๊ฒฐ๊ณผ: ['a', 'b', 'c', 1, 2, 3]
//๋ฐฐ์ด ์ธ๊ฐ ์ด์ด๋ถ์ด๊ธฐ
const num1 = [1, 2, 3];
const num2 = [4, 5, 6];
const num3 = [7, 8, 9];
num1.concat(num2, num3);
// ๊ฒฐ๊ณผ: [1, 2, 3, 4, 5, 6, 7, 8, 9]
filter() ํจ์
- filter()์ ์ธ์๋ก ๋๊ฒจ์ง๋ callbackํจ์์
ํ ์คํธ(์กฐ๊ฑด)๋ฅผ ํต๊ณผํ๋ ์์๋ฅผ ๋ชจ์ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑ
- filter()ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์์ ์ํ๋ ๊ฐ์ ์ญ์ ํ๋ ์ฝ๋ ๊ตฌํ ๊ฐ๋ฅ
1. animal์์ ๊ธธ์ด๊ฐ 3 ์ด์์ธ animals์ return ํ๋ ํจ์!
function Filter() {
const animals = ["dog", "cat", "rabbit"];
return (
<>
<div>
{animals.filter((aaa) => {
return aaa.length > 3;
})}
</div>
</>
);
}
2. ์ํ๋ฒณ ๋๋ธ ํด๋ฆญ ํ์ ๋ ์ํ๋ฒณ ์ญ์ ํ๊ธฐ
const deleteAlpha = (id) => {
//์ผ์น ํ์ง ์๋ ๊ฒ๋ง ๋นผ๊ณ list์ ๋ด์
const newAlpha = list.filter((value) => value.id !== id);
setList(newAlpha);
console.log(newAlpha);
};
...
return (
<li key={value.id} onDoubleClick={() => deleteAlpha(value.id)}>
{value.alpha}
</li>
);
ref
html์์ ์์๋ฅผ ์ ํํ ๋
id๋ฅผ ์ง์ ํ ํ ์์๋ฅผ ํ์ถํ๋ค.
<div id = "id1">๋ด์ฉ</div>
๊ฐ์ ์ปดํฌ๋ํธ๋ฅผ ๋ฐ๋ณตํด์ ์ฌ์ฉํ๋ฉด id ๊ฐ ์ค๋ณต๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์ด๊ฒ์ ํด๊ฒฐํ๊ธฐ ์ํด ref๋ฅผ ์ฌ์ฉ!
ref๋ ์ปดํฌ๋ํธ ๋ด๋ถ์์๋ง ์๋๋๊ธฐ ๋๋ฌธ์ด๋ค!
ref๋? reference
- ์ ์ญ์ ์ผ๋ก ์๋ํ์ง ์๊ณ ์ปดํฌ๋ํธ ๋ด๋ถ์์ ์ ์ธ ๋ฐ ์ฌ์ฉ
- ๋์ผํ ์ปดํฌ๋ํธ๋ฅผ ๋ฐ๋ณตํด ์ฌ์ฉํด๋
๊ฐ ์ปดํฌ๋ํธ ๋ด๋ถ์์๋ง ๋์ํ๋ค.
- dom ์ ์ง์ ์ ์ผ๋ก ๊ฑด๋๋ ค์ผ ํ ๋ ์ฌ์ฉํ๋ค.
- ref prop
: html์์์ ๋ ํผ๋ฐ์ค๋ฅผ ref prop์ ์ ์ฅ
<element ref={myRef}></element>
Ref ์ฌ์ฉํ๊ธฐ (ํจ์ํ ์ปดํฌ๋ํธ)
useRef()
: ํจ์ํ ์ปดํฌ๋ํธ์์๋ useRef()๋ฅผ ์ด์ฉํ์ฌ Dom ์์์ ์ง์ ์ ๊ทผํ๋ค.
//1. ref ๊ฐ์ฒด ๋ง๋ค๊ธฐ
const myRef = useRef();
//2. ์ ํํ๊ณ ์ถ์ dom์ ref๊ฐ์ผ๋ก ์ค์
<element ref={myRef}></element>
//3. useRef()๋ก ๋ง๋ ๊ฐ์ฒด ์์ current ๊ฐ ์ค์ ์์๋ฅผ ๊ฐ๋ฅดํด
myRef.current;
๋ฌธ์ 1. ํจ์ํ ์ปดํฌ๋ํธ์์ ๋ฒํผ ํด๋ฆญ ์ input focusing
function RefSample() {
//1. ref๊ฐ์ฒด ๋ง๋ค๊ธฐ
const inputRef = useRef();
const handleFocus = () => {
//3. useRef()๋ก ๋ง๋ ๊ฐ์ฒด์ current ๊ฐ์ผ๋ก ์ ๊ทผ
//focus(): ์ง์ ๋ html์์์ ํฌ์ปค์ค ์ค์
inputRef.current.focus();
};
return (
<>
<p>ํจ์ํ ์ปดํฌ๋ํธ์์ ๋ฒํผ ํด๋ฆญ ์ input focusing</p>
{/* 2. ์ง์ ์ ๊ทผํด์ผ ํ๋ dom ์์์ ref prop์ค์ */}
<input ref={inputRef} type="text" />
<button onClick={handleFocus}>focus</button>
</>
);
}
2. ref๋ฅผ ์ฌ์ฉํ์ฌ ๋ฒํผ ํด๋ฆญ์ 1์ฉ ์ฆ๊ฐ
function RefSample2() {
//1. useRef()๋ฅผ ์ฌ์ฉํ์ฌ ๋ก์ปฌ ๋ณ์ ๋ง๋ค๊ธฐ
const idRef = useRef(1);
const plusId = () => {
idRef.current += 1;
console.log(idRef.current);
};
return (
<>
<h1>{idRef.current}</h1>
<button onClick={plusId}>plus ref</button>
</>
);
}
3๋ฒ.
const [inputWriter, setInputWriter] = useState("");
const [inputTitlt, setInputTitle] = useState("");
const writerInputElem = useRef();
const titleInputElem = useRef();
...
//input ๋น ๊ฐ ๊ฒ์ฌ
const checkInput = () => {
//input์ ๊ฐ์ด ์์ผ๋ฉด ํด๋น input focusing
if (inputWriter.trim().length === 0) {
writerInputElem.current.focus();
return false;
}
if (inputTitlt.trim().length === 0) {
titleInputElem.current.focus();
return false;
}
return true;
};
...
<input
ref={writerInputElem}
value={inputWriter}
...
- trim : ๋ฌธ์์ด์์ ๋ ฅ๋ฐ๊ฑฐ๋ ๋ฌธ์์ด์ ํฉ์น ๊ฒฝ์ฐ
์ธ๋ฐ ์๋ ๊ณต๋ฐฑ์ด ๋ฐ๋ผ๋ค์ด๊ฐ์ง ์๋๋ก ์ฐ๋ ํจ์
//trimํจ์ ์ฌ์ฉ ์์
String str = " ๋ฌธ์์ด์ ๊ณต๋ฐฑ์ด ์์ต๋๋ค. ";
System.out.println(str);
str = str.trim();
System.out.println(str);
Ref ์ฌ์ฉํ๊ธฐ (ํด๋์คํ ์ปดํฌ๋ํธ)
1. ์ฝ๋ฐฑ ํจ์
- ์ฌ์ฉํ๊ณ ์ ํ๋ DOM ์์์ ref๋ผ๋ ์ฝ๋ฐฑ ํจ์ ์์ฑ ๋ฐ props๋ก ์ ๋ฌ
- ref ๋ฅผ ์ปดํฌ๋ํธ์ ๋ฉค๋ฒ ๋ณ์๋ก ์ค์ ํ๋๋ฐ,
์ด๋ ref๋ ์ํ๋ ๋๋ก ์ฌ์ฉ ๊ฐ๋ฅ
<input ref={(ref=> {this.input = ref} />
Life Cycle
- ๋ผ์ดํ ์ฌ์ดํด
- ์๋ช ์ฃผ๊ธฐ
- ๋ชจ๋ react component์ ์กด์ฌํ๋ ๊ฒ
- Mount
: dom ์ด ์์ฑ๋๊ณ
์น ๋ธ๋ผ์ฐ์ ์์ ๋ํ๋จ
- Update
: props or state๋ฐ๋์์ ๋ ์ ๋ฐ์ดํธ ํจ
- UnMount
: ์ปดํฌ๋ํธ๊ฐ ํ๋ฉด์์ ์ฌ๋ผ์ง(์ ๊ฑฐ๋ ) ๋
LifeCycle ํจ์ํ ์ปดํฌ๋ํธ
useEffect()
- ํจ์ํ ์ปดํฌ๋ํธ์์๋ useEffect()๋ฅผ ์ฌ์ฉํด
Mount, Update, Unmount ์ ํน์ ์ก์ ์ ์ฒ๋ฆฌํจ
//์ปดํฌ๋ํธ Mount or Update๋ ๋ ๋์
useEffect(() => {});
//์ปดํฌ๋ํธ Mount๋ ๋ ๋์
useEffect(() => {}, []);
//data ๊ฐ์ด Update ๋ ๋ ๋์
const [data, setData] = useState();
useEffect(()=>{}, [data]);
//text๊ฐ ๋ณ๊ฒฝ๋ ๋
useEffect(() => {
console.log("Text | Update");
}, [text]);
//3 ์ด ํ post๋ณด์ฌ์ฃผ๊ธฐ
useEffect(() => {
setTimeout(() => {
getPost();
}, 3000);
}, []);
LifeCycle ํด๋์คํ ์ปดํฌ๋ํธ
๋ง์ดํธ ๋ ๋ ํธ์ถ๋๋ ๋ฉ์๋
๋ง์ดํธ (Mount)
- constructor
- render
- getDerivedStateFromProps
- componentDidMount
์ ๋ฐ์ดํธ(update)
: ์ ๋ฐ์ดํธ ๋ ๋ ํธ์ถ๋๋ ๋ฉ์๋
- getDerrivedStateFromProps
- shouldComponentUpdate
- componentDidUpdate
์ธ๋ง์ดํธ(unmount)
- componentWillUnmount
-> ํ์ฌ์๋ ํด๋์คํ ์ปดํฌ๋ํธ๋ ์ ์ฌ์ฉํ์ง ์์!
changeNumber = () => {
this.setState({ number: this.state.number + 1 });
};
changeVisible = () => {
this.setState({ visible: !this.state.visible });
};
.
.
.
์ด๋ง!