Why does this fetched print twice? When the axios call is put outside the function it only runs once.
function App() {
axios('https://www.google.com/').then(x =>
console.log("fetched")
)
return (
<div className="App">
</div>
);
}
export default App;
Do it in a useEffect
hook.
function App() {
React.useEffect(() => {
axios('https://www.google.com/').then(x =>
console.log("fetched")
)
}, [])
return (
<div className="App">
</div>
);
}
export default App;
If you want a call to run only one time when the component is rendered, you should put it inside a useEffect.
useEffect(() => {
axios('https://www.google.com/').then(x =>
console.log("fetched")
);
},[]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.