TypeError:urls.map 不是 React 中的函数
2021-03-20
492
我在 React 中显示图像时遇到问题。从 Firebase 存储中获取的所有图像 URL 列表。传递所有 URL 显示图像显示 urls.map 不是函数
const ViewImages = () => {
var storageRef = firebase.storage().ref("images/");
const [image, setImage] = useState(""); const [urls, setFiles] = useState("");
const [imageUrl, setImageUrl] = useState([]);
useEffect(() => { const fetchImages = async () => {
let result = await storageRef.child('images1/').listAll();
let urlPromises = result.items.map(imageRef => imageRef.getDownloadURL());
return Promise.all(urlPromises);
//console.log(urlPromises);
}
const loadImages = async () => {
const urls = await fetchImages();
setFiles(urls);
console.log(urls[0]);
}
loadImages();
}, []);
return(
{urls.map((urls,i) => (
<img src={urls} key={i}/>
))}
</div>
);
>
export default ViewImages;
1个回答
问题是您在空字符串 (
''
) 上调用
map
方法,而该字符串是
urls
的初始值。请将其设置为空数组。
const [urls, setFiles] = useState([])
Arun Kumar Mohan
2021-03-20