Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์ฑ
- ๋ฐ์ํ
- ๋ง์ผ๋ด๊ฐ์ธ์์๋ค์์ฐ๋ค๋ฉด
- ๋ฆฌ์กํธ
- ๋ ์
- Java
- ComputerScience
- ์ฝ๋ฉ
- ์๋ฐ์คํฌ๋ฆฝํธ
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- ์นํผ๋ธ๋ฆฌ์ฑ
- ๋ผํ๋ผ์ค์๋ง๋
- K๋ฐฐํฐ๋ฆฌ
- ์นํ์ด์ง๋ง๋ค๊ธฐ
- html
- ์ค๋ธ์
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ์ํ
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- database
- ์๋ฐ
- Python
- ์ค๋ผํด
- css
- ๊ฐ๋ฐ
- ํ์ด์ฌ
- ํ๋ก๊ทธ๋๋ฐ
- ์ปดํจํฐ๊ณผํ
- JavaScript
Archives
- Today
- Total
JiYoung Dev ๐ฅ
[React] Spring Boot์ ์ฐ๋ํ์ฌ shop ๋ง๋ค๊ธฐ (2023.06.09) ๋ณธ๋ฌธ
full stack/React
[React] Spring Boot์ ์ฐ๋ํ์ฌ shop ๋ง๋ค๊ธฐ (2023.06.09)
Shinjio 2023. 6. 9. 17:47
Spring Boot์ ์ฐ๋ํ์ฌ shop ๋ง๋ค๊ธฐ
CORS ์ค๋ฅ (Cross Origin Resource Sysytem)
๊ต์ฐจ ์ถ์ฒ ๋ฆฌ์์ค ๊ณต์
proxy ์๋ฒ(๋ฏธ๋ค์จ์ด)๋ก ํด๊ฒฐ์ ํ์
setupProxy.js ๊ฒ์
ํ๋ก์ ์๋ฒ๋ ํด๋ผ์ด์ธํธ์ ์๋ฒ ์ฌ์ด์์ ๋ฐ์ดํฐ ์์ฒญ๊ณผ ์๋ต์ ๋์ ๋ฐ์์ฃผ๋ ์ญํ ์ ํด์ค
๋ณด์์์ ๋ฌธ์ ๋ก ์ง์ ํต์ ์ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ ๊ฒฝ์ฐ ํ๋ก์ ์๋ฒ๊ฐ ์ค๊ณ๋ฅผ ์งํํด์ค
๋ง๋ค๋ src ํ์์ ํด๋น ํ์ผ์ ๋ง๋ค์ด์ผ ํจ!
< ์ฐธ๊ณ ํ ๋ฌธ์ >
https://velog.io/@yunsungyang-omc/React-React-App%EC%97%90%EC%84%9C-CORS-%EC%9D%B4%EC%8A%88-%ED%95%B4%EA%B2%B0%ED%95%98%EA%B8%B0
axios๋ก spring boot ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
useEffect(() => {
axios.get("http://172.30.1.42:8090/shop/").then((res) => {
console.log("๊ฒฐ๊ณผ", res.data);
setList(res.data);
});
}, []);
์ฃผ์๋ฅผ ๊ฐ์ ธ์ฌ ๋ local ํธ์คํธ๋ก ๊ฐ์ ธ์ฌ ์ ์์!
IP์ฃผ์๋ก ๊ฐ์ ธ์ค๊ธฐ
useSearchParams ์ฌ์ฉํ์ฌ ํํฐ ๊ธฐ๋ฅ ์ถ๊ฐ
ProductList.jsx
import React, { useState } from 'react'
import { useNavigate, useSearchParams } from 'react-router-dom';
const ProductList = ({list}) => {
console.log('props', list);
// type filter ์์
์ ๊ฑฐ์น๊ณ ์ฌ์ฉํ list
/**๋ง์ฝ, '์ํผ์ค' ํ์ด์ง๋ฅผ ํด๋ฆญํ๋ฉด type์ด dress์ธ๊ฒ๋ง
* '์ ์ฒด' ํ์ด์ง๋ฅผ ํด๋ฆญํ๋ฉด ์ ์ฒด ๋์ด์ฃผ๊ธฐ
* useSearchParams ํ์ฉ
*/
const[typeParams, setTypeParams] = useSearchParams()
let type = typeParams.get('type');
//type filter ์์
์ ๊ฑฐ์น๊ณ ์ฌ์ฉํ list
let newList = type != null && list.filter(item => item.product.ptype == 'dress')
const nav = useNavigate()
return (
<div>
{/* Q.
์ํ๋ช
- ๊ฐ๊ฒฉ์ 10๊ฐ ์ํ ๋ชจ๋ ๋์์ค ๊ฒ
*/}
{type == null
? list.map((item)=>(
<div key={item.product.pcode} onClick={()=>{nav(`/product/${item.product.pcode}`)}}>
<img src={"data:image/;base64," + item.product.img} width='100px'></img>
<p><strong>{item.product.pname}</strong> {item.product.price}</p>
</div>
)): newList.map((item)=>(
<div key={item.product.pcode}>
<img src={"data:image/;base64," + item.product.img} width='100px'></img>
<p><strong>{item.product.pname}</strong> {item.product.price}</p>
</div>
))}
</div>
)
}
export default ProductList
ํ๋์ ์ํ์ ๋ํ ์ ํ ์ ๋ณด spring boot์์ ๊ฐ์ ธ์ค๊ธฐ
useParam ์ฌ์ฉ
ํ์ฌ ์คํ๋ง ๋ถํธ๋ฅผ ํตํด /pcode ๊ฒฝ๋ก ์ ์ํ๋ฉด pcode์ ํด๋นํ๋ ์ํ 1๊ฐ์ ์ ๋ณด๋ง ๋ณด์ฌ์ค
ProductDetail.jsx
import React, { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import axios from "axios";
const ProductDetail = () => {
let {num} = useParams()
console.log('num', num);
const [obj, setObj] = useState({});
// axios๋ฅผ ํตํด์ ํ๊ฐ์ง ์ํ์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์ฌ ๊ฒ
useEffect(() => {
axios.get(`http://172.30.1.42:8090/shop/${num}`)
.then((res) => {
console.log("detail ๊ฒฐ๊ณผ", res.data.product);
setObj(res.data.product)
});
}, []);
// ์ง์ง ์์ดํ
์ด ์ ๋ณํ๋ ํ์ธํ๊ณ ์ถ์ผ๋ฉด ?
// useEffect(()=>{
// console.log(obj);
// }, [obj])
return (
<div>
<div>
<img src={"data:image/;base64," + obj.img} width='100px'></img>
<p><strong>{obj.pname}</strong> {obj.price}</p>
</div>
</div>
)
}
export default ProductDetail
์ฃผ์์
UseEffect ์ฒ๋ฆฌ ์ํด์ฃผ๋ฉด?
๋ฌดํ๋ฃจํ ๋๊ฑฐ๋ ๊ทธ ์ด์ ์ ์ ํํ ๊ฐ์ด ๋ํ๋จ(์์ ์ด ์๋ง๋๋ค)
App.js
import { useEffect, useState } from "react";
import "./App.css";
import Header from "./components/Header";
import ProductDetail from "./components/ProductDetail";
import ProductList from "./components/ProductList";
import { Route, Routes } from "react-router-dom";
import axios from "axios";
function App() {
const [list, setList] = useState([]);
useEffect(() => {
axios.get("http://172.30.1.42:8090/shop/").then((res) => {
console.log("๊ฒฐ๊ณผ", res.data);
setList(res.data);
});
}, []);
return (
<div className="App">
<Header />
<Routes>
<Route path="/" element={<ProductList list={list} />}></Route>
<Route path="/product/:num" element={<ProductDetail />}></Route>
</Routes>
</div>
);
}
export default App;