개발일지 TIL(Today I Learned)
[2023.07.06] 개발일지
이승재(Frontend Developer)
2023. 7. 9. 22:57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import { useNavigate, useParams } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { styled } from 'styled-components';
function Detail() {
const todos = useSelector((state) => {
return state.todos
});
const todo = todos.filter((todo) => todo.id === id)[0];
const navigate = useNavigate();
return (
<LayOut2>
<LayOutBox>
<FirstBox>
<IdBox>id : {todo.id}</IdBox>
<BackButton onClick={() => {
navigate('/');
}}
>
이전으로
</BackButton>
</FirstBox>
<br />
<TextBox>{todo.title}</TextBox>
<br />
{todo.body}
</LayOutBox>
</LayOut2>
)
}
|
cs |
{todo.id}값, {todo.title}값과 {todo.body}값을 Detail 페이지에 출력하기 위한 코드를 짰지만
값들이 출력이 되지 않고 오류만 난다.
여기서 내가 간과한 것이 useSelector로 가져온 todo배열의 객체들의 고유 id값이 이 컴포넌트 내에는 없다는 것이다.
그래서 여기에 useParams 훅을 사용해