인프런 커뮤니티 질문&답변
질문입니다
작성
·
169
0
파일의 정보를 state를 통해 [...state, res.data.path] 형식으로 저장하려는 로직에서 자꾸 에러가 나는데 왜 그런 것 일까요??
\client\src\components\views\Utils\Fileupload.js
function Fileupload(props) {
const [Images, setImages] = useState([]);
const dropHandler = (files)=>{
// if you need to send file format
console.log(files);
let formData = new FormData();
const config = {
header:{'content-type':'multipart/form-data'}
}
formData.append('file',files[0]);
axios.post('/api/product/image',formData,config)
.then(response=>{
if(response.data.success){
// to use this image info later we need to store it in state
console.log(response.data);
setImages([...Images, response.data.filePath]);
props.refreshFunction(Images);
}else{
alert('failed to save the files in server')
}
})
}
위 코드에서 setImages([...Images, response.data.filePath]); 가 정상적으로 동작하지 않는 것을 확인했습니다.
고쳐보려 수 차례 생각해봤는데 잘 안되네요 ㅠ
감사합니다!!
git : https://github.com/JoonsongLee/q2.git
답변 1
0
andrewlee
질문자
대충 제끼고 나서 다시 한번 코드보니
props.refreshFunction(Images)를 props.refreshFunction([...Images,response.data.filPath])로 하니 정상 작동이 되는데
이 이유가 무엇일까요..? 배열은 스테이트로는 못 넘기는 건가요?





