此问题在此处已有答案:
why componentdidmount called two times(3个答案)
2天前关闭。
我只是在component did mount内部调用fetch方法,当我控制结果时,它被打印了两次,而不是逻辑上打印一次。
她是组件代码
import React,{Component} from "react";
import './DealComponent.css';
class DealComponent extends Component {
constructor(){
super();
this.state = {
photos: []
}
}
componentDidMount(){
fetch("https://api.thedogapi.com/v1/images/search?size=med&mime_types=jpg&format=json&has_breeds=true&order=RANDOM&page=0&limit=5")
.then(response=>{
if(!response.ok){
throw Error("Error fetching the cute dog");
}
return response.json();
}).then(allData=>{
console.log(allData);
this.setState({
...this.state,
photos:allData
})
}).catch(err=>{
throw Error(err.message);
})
}
render(){
return (
<div className="Deal-Component">
<div className="Second-Div">
<div className="Header-Div">
<a href="https://www.gopaisa.com/" target="_blank">
<img className="Logo Logo1" src="https://www.gopaisa.com/images/gopaisa_new_logo.png" alt="Big Fashion Days - Gopaisa Exclusive"/>
</a>
<a href="https://play.google.com/store/apps/details?id=com.gopaisa" target="_blank">
<img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/categories/Download_Button.png" alt="Big Fashion Days - Gopaisa Exclusive" className="Logo"/>
</a>
</div>
<div className="Body-Container-Div">
<a href="https://www.gopaisa.com/cosmetics-offers-coupons-sale-deals" target="_blank">
<img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/the-great-indian-wedding-sale-29-11-022-0-0.jpg" alt="The Great Indian Wedding Sale" className="Logo"/>
</a>
<a href="https://www.gopaisa.com/jewellery-gifts-offers-coupons/malabar-gold-diamonds-jewellery-discount-offer" target="_blank"><img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/gpwedding-jewellery-landing-page-banner-28112022 (1).png" alt="The Great Indian Wedding Sale" className="Logo"/></a>
<a href="https://www.gopaisa.com/electronics" target="_blank"><img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/Slice 3.png" alt="The Great Indian Wedding Sale" className="Logo"/></a>
<a href="https://www.gopaisa.com/clothing-sale-deals-coupons-offers" target="_blank"><img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/clothing-nov-29-11-2022.png" alt="The Great Indian Wedding Sale" className="Logo"/></a>
<a href="https://www.gopaisa.com/travel-offers-deals-coupons" target="_blank"><img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/Slice 5.png" alt="The Great Indian Wedding Sale" className="Logo"/></a>
<a href="https://www.gopaisa.com/everyday-electronics-sale-deals-coupons-offers" target="_blank"><img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/Slice 6.png" alt="The Great Indian Wedding Sale" className="Logo"/></a>
<a href="https://www.gopaisa.com/mobiles" target="_blank"><img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/Slice 7.png" alt="The Great Indian Wedding Sale" className="Logo"/></a>
<a href="https://www.gopaisa.com/Women-Inner-wear-deals-offers" target="_blank"><img src="https://gpcdn.ams3.cdn.digitaloceanspaces.com/deals/Slice 8.png" alt="The Great Indian Wedding Sale" className="Logo"/></a>
</div>
</div>
</div>
)
}
}
export default DealComponent ;
我试图删除fetch的then方法中的setstate(),但fetch请求仍然被命中两次,因为它在devtools控制台中打印了两次控制台语句。非常感谢任何帮助。
1条答案
按热度按时间wfsdck301#
从<React.StrictMode>index.js中删除
您的index.js将看起来像