๐ฅ ๋ฒจ๋กํผํธ์ ํจ๊ปํ๋ ๋ชจ๋ ๋ฆฌ์กํธ
๋ถ๋ณ์ฑ !!!!
push, splice XXX
concat, filter, map OOO
11. ๋ฐฐ์ด ๋ ๋๋งํ๊ธฐ
map() : ๋ฐฐ์ด์์ ์๋ ๊ฐ ์์๋ฅผ ๋ณํํ์ฌ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด ์ค
๊ฐ ์์๋ค๋ง๋ค ๊ณ ์ ํ key ๊ฐ์ ์ค์ ํด์ค์ผ ๋จ
=> ๊ทธ๋์ผ์ง, ์์ ๋์ง ์๋ ๊ธฐ์กด๊ฐ์ ๊ทธ๋๋ก ๋๊ณ ์ํ๋ ๊ณณ์ ๋ด์ฉ์ ์ฝ์ ํ๊ณ ์ญ์ ํจ
<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} key={user.id} />
))}
</div>
);
}
export default UserList;
12. useRef๋ก ์ปดํฌ๋ํธ ์์ ๋ณ์ ๋ง๋ค๊ธฐ
useRef๋ก ๊ด๋ฆฌํ๋ ๋ณ์๋ ๊ฐ์ด ๋ฐ๋๋ค๊ณ ํด์ ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง ๋์ง ์๋๋ค.
=> ์ค์ ํ ๋ฐ๋ก ์กฐํํ ์ ์๋ค.
<App.js>
import React, { useRef } from 'react';
import UserList from './UserList';
function App() {
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'
}
];
const nextId = useRef(4);
const onCreate = () => {
// ๋์ค์ ๊ตฌํ ํ ๋ฐฐ์ด์ ํญ๋ชฉ ์ถ๊ฐํ๋ ๋ก์ง
// ...
nextId.current += 1;
};
return <UserList users={users} />;
}
export default App;
useRef์ ํ๋ผ๋ฏธํฐ = .current์ ๊ธฐ๋ณธ๊ฐ
์ด ๊ฐ์ ์์ ํ ๋์๋ .current ๊ฐ์ ์์ ํ๋ฉด ๋๊ณ , ์กฐํํ ๋์๋ .current ๊ฐ์ ์กฐํํ๋ฉด ๋จ
<UserList.js>
import React from 'react';
function User({ user }) {
return (
<div>
<b>{user.username}</b> <span>({user.email})</span>
</div>
);
}
function UserList({ users }) {
return (
<div>
{users.map(user => (
<User user={user} key={user.id} />
))}
</div>
);
}
export default UserList;
13. ๋ฐฐ์ด ํญ๋ชฉ ์ถ๊ฐํ๊ธฐ onCreate
concat : ๊ธฐ์กด์ ๋ฐฐ์ด์ ์์ ํ์ง ์๊ณ , ์๋ก์ด ์์๊ฐ ์ถ๊ฐ๋ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด ์ค
<CreateUser.js>
import React from 'react';
function CreateUser({ username, email, onChange, onCreate }) {
return (
<div>
<input
name="username"
placeholder="๊ณ์ ๋ช
"
onChange={onChange}
value={username}
/>
<input
name="email"
placeholder="์ด๋ฉ์ผ"
onChange={onChange}
value={email}
/>
<button onClick={onCreate}>๋ฑ๋ก</button>
</div>
);
}
export default CreateUser;
<App.js>
import React, { useRef, useState } from 'react';
import UserList from './UserList';
import CreateUser from './CreateUser';
function App() {
const [inputs, setInputs] = useState({
username: '',
email: ''
});
const { username, email } = inputs;
const onChange = e => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value
});
};
const [users, setUsers] = useState([
{
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'
}
]);
const nextId = useRef(4);
const onCreate = () => {
const user = {
id: nextId.current,
username,
email
};
setUsers(users.concat(user));
setInputs({
username: '',
email: ''
});
nextId.current += 1;
};
return (
<>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} />
</>
);
}
export default App;
14. ๋ฐฐ์ด ํญ๋ชฉ ์ ๊ฑฐํ๊ธฐ onRemove
filter : ๋ฐฐ์ด์์ ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์๋ค๋ง ์ถ์ถํ์ฌ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด ์ค
<UserList.js>
import React from 'react';
function User({ user, onRemove }) {
return (
<div>
<b>{user.username}</b> <span>({user.email})</span>
<button onClick={() => onRemove(user.id)}>์ญ์ </button>
</div>
);
}
function UserList({ users, onRemove }) {
return (
<div>
{users.map(user => (
<User user={user} key={user.id} onRemove={onRemove} />
))}
</div>
);
}
export default UserList;
onRemove : id๊ฐ ~์ธ ๊ฐ์ฒด๋ฅผ ์ญ์ ํด๋ผ
<App.js>
import React, { useRef, useState } from 'react';
import UserList from './UserList';
import CreateUser from './CreateUser';
function App() {
const [inputs, setInputs] = useState({
username: '',
email: ''
});
const { username, email } = inputs;
const onChange = e => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value
});
};
const [users, setUsers] = useState([
{
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'
}
]);
const nextId = useRef(4);
const onCreate = () => {
const user = {
id: nextId.current,
username,
email
};
setUsers(users.concat(user));
setInputs({
username: '',
email: ''
});
nextId.current += 1;
};
const onRemove = id => {
// user.id ๊ฐ ํ๋ผ๋ฏธํฐ๋ก ์ผ์นํ์ง ์๋ ์์๋ง ์ถ์ถํด์ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ฌ
// = user.id ๊ฐ id ์ธ ๊ฒ์ ์ ๊ฑฐํจ
setUsers(users.filter(user => user.id !== id));
};
return (
<>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onRemove={onRemove} />
</>
);
}
export default App;
15. ๋ฐฐ์ด์ ํญ๋ชฉ ์์ ํ๊ธฐ onToggle
<App.js>
import React, { useRef, useState } from 'react';
import UserList from './UserList';
import CreateUser from './CreateUser';
function App() {
const [inputs, setInputs] = useState({
username: '',
email: ''
});
const { username, email } = inputs;
const onChange = e => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value
});
};
const [users, setUsers] = useState([
{
id: 1,
username: 'velopert',
email: 'public.velopert@gmail.com',
active: true
},
{
id: 2,
username: 'tester',
email: 'tester@example.com',
active: false
},
{
id: 3,
username: 'liz',
email: 'liz@example.com',
active: false
}
]);
const nextId = useRef(4);
const onCreate = () => {
const user = {
id: nextId.current,
username,
email
};
setUsers(users.concat(user));
setInputs({
username: '',
email: ''
});
nextId.current += 1;
};
const onRemove = id => {
// user.id ๊ฐ ํ๋ผ๋ฏธํฐ๋ก ์ผ์นํ์ง ์๋ ์์๋ง ์ถ์ถํด์ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ฌ
// = user.id ๊ฐ id ์ธ ๊ฒ์ ์ ๊ฑฐํจ
setUsers(users.filter(user => user.id !== id));
};
const onToggle = id => {
setUsers(
users.map(user =>
user.id === id ? { ...user, active: !user.active } : user
)
);
};
return (
<>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onRemove={onRemove} onToggle={onToggle} />
</>
);
}
export default App;
<UserList.js>
import React from 'react';
function User({ user, onRemove, onToggle }) {
return (
<div>
<b
style={{
cursor: 'pointer',
color: user.active ? 'green' : 'black'
}}
onClick={() => onToggle(user.id)}
>
{user.username}
</b>
<span>({user.email})</span>
<button onClick={() => onRemove(user.id)}>์ญ์ </button>
</div>
);
}
function UserList({ users, onRemove, onToggle }) {
return (
<div>
{users.map(user => (
<User
user={user}
key={user.id}
onRemove={onRemove}
onToggle={onToggle}
/>
))}
</div>
);
}
export default UserList;
'โ๏ธ > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React ๊ณต๋ถํ๊ธฐ(4) - API ์ฐ๋ํ๊ธฐ (0) | 2021.11.05 |
---|---|
React ๊ณต๋ถํ๊ธฐ(2) - styled-components (0) | 2021.10.15 |
React ๊ณต๋ถํ๊ธฐ(1) - Hooks ์ข ๋ฅํธ (0) | 2021.10.10 |
React ๊ณต๋ถํ๊ธฐ(1) - ์ํ๊ด๋ฆฌํธ (0) | 2021.10.10 |
React ๊ณต๋ถํ๊ธฐ(1) - ๊ธฐ๋ณธํธ (0) | 2021.10.10 |