-
글쓴이글
-
2021년 3월 11일 17:42 #7063
네이버시스템참가자제가 하는 방식이 맞는지 모르겠어요
localStorage에 배열로 저장한거까지 나와서 그뒤로는 제가 해봤는데요let [watched, watched변경]= useState([]); 를 만들고
useEffect가장 아래에 watched변경([...arr]);을 하고
ui에 <div className="col-md-4">
<p width="100%" >최근 본 상품</p>
<Watched watched={watched}></Watched>
</div>
Watched 컴포넌트는
function Watched(props){
return (
<div>
{props.item[0].title}
<Table responsive>
<thead>
<tr>
<th>이미지</th>
<td>상품명</td>
</tr>
</thead>
<tbody>
{
props.watched.map((a, i)=>{
return (
<tr key={i}>
<td><img src={'https://codingapple1.github.io/shop/shoes'+(a)+'.jpg'} width="50px" /></td>
<td>????</td>
</tr>
)
})
}
</tbody>
</Table>
</div>
)
}
이렇게 했는데 상품명에 저장된 a로 상품명을 가져오는 법을 모르겠어서 redux로 index페이지에 data.js를 추가해서
let shoes초기값 = Data;function reducer3(state = shoes초기값){
return state;
}
이렇게 reducer3을 만들고</li>
function state를props화(state){
console.log(state)
return {
state : state.reducer, //stete안에 있는 모든 데이터를 state라는 이름의 props로 바꿔주세용
//이러면 state라고 쓰는 순간 안의 모든 데이터가 출력이 됨
alert열렸니 : state.reducer2,
item : state.reducer3
}
}
추가를 하고
function Watched(props){
return (
<div>
<Table responsive>
<thead>
<tr>
<th>이미지</th>
<td>상품명</td>
</tr>
</thead>
<tbody>
{
props.watched.map((a, i)=>{
return (
<tr key={i}>
<td><img src={'https://codingapple1.github.io/shop/shoes'+(a)+'.jpg'} width="50px" /></td>
<td>{props.item[JSON.parse(a)].title}</td>
</tr>
)
})
}
</tbody>
</Table>
</div>
)
}
watched 펑션에 <td>{props.item[JSON.parse(a)].title}</td> 이렇게 넣으면
따옴표를 떼고 a가 들어가서 data.js의 title을 가져올줄알았는데 안되네요
시작부터 잘못한거같은데 어떻게 해야할까요..2021년 3월 11일 17:53 #7064
네이버시스템참가자<td>{props.item[JSON.parse(a)].title}</td> 이거 하기전에는 로컬스토리지에 저장된 이미지가 잘 출력이 되는데
이걸 추가하면 ×TypeError: Cannot read property '2' of undefined 이렇게 나옵니다!
2021년 3월 11일 19:40 #7066
codingapple키 마스터<Watched> 컴포넌트에서 props.item 을 쓰려고 하는데 props.item 이라는 항목이 없다는 것 같은데
부모컴포넌트에서 <Watched>까지 item이라는 props를 잘 전달하도록 합시다
그리고 a라는게 watched라는 state에 저장된 있는 1,2 이런 상품번호 맞죠
숫자 자료는 JSON.parse할 필요가 없어보입니다 parse는 JSON자료를 object로 변환해주는 함수라서요
-
글쓴이글
- 답변은 로그인 후 가능합니다.