티스토리 뷰
1. simple way using destructuring
const AdminProductDetail = () => {
const params = useParams();
const navigate = useNavigate();
const [product, setProduct] = useState({
name: '',
price: 0,
image: '',
brand: '',
category: '',
countInStock: 0,
description: '',
});
const getData = async (id) => {
try {
const { data } = await axios.get(`/api/products/${id}`);
const { name, price, image, brand, category, countInStock, description } = data;
setProduct({
name,
price,
image,
brand,
category,
countInStock,
description,
});
} catch (error) {
console.log(error);
}
};
2. could be better when not many states are needed
const AdminProductDetail = () => {
const params = useParams();
const navigate = useNavigate();
const [name, setName] = useState('');
const [price, setPrice] = useState(0);
const [image, setImage] = useState('');
const [brand, setBrand] = useState('');
const [category, setCategory] = useState('');
const [countInStock, setCountInStock] = useState(0);
const [description, setDescription] = useState('');
const getData = async (id) => {
try {
const { data } = await axios.get(`/api/products/${id}`);
console.log('getsingledata', data);
setName(name);
setPrice(price)
setImage(image)
setBrand(brand)
setCategory(category)
setCountInStock(countInStock)
setDescription(description)
} catch (error) {
console.log(error);
}
};
'React.js' 카테고리의 다른 글
tailwind css사용시 input에 outline border가 사라지지 않을 경우 (0) | 2023.01.10 |
---|---|
마우스클릭이벤트 click과 mousedown의 차이점 (0) | 2023.01.06 |
input data 를 관리하는 3가지 방법 (0) | 2022.11.23 |
Intro to React.js (0) | 2019.08.29 |
Setting up React.js Environment (0) | 2019.08.29 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Object type casting
- javascript
- compareTo()
- 프로그래머스
- algorithm
- hackerrank javascript solution
- string class in java
- spring boot application
- ... in Javascript
- Javascript Algorithm
- easy javascript algorithm
- Collection Framework
- equals()
- easy algorithm
- math.max
- hackerrank solution
- 프로그래머스 알고리즘문제
- hackerrank
- 알고리즘
- repeat()
- 프로그래머스 알고리즘
- math.abs
- HashMap
- C++
- rest parameter
- HackerRank Algorithm
- java
- code refactoring
- substring()
- hackerrank javascript
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함