1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
加載資源后,您永遠(yuǎn)不會(huì)將isLoading狀態(tài)設(shè)置回原來的狀態(tài)false
const AppProvider = ({ children }) => {
const [isLoading, setIsLoading] = useState(true);
const [isError, setError] = useState({ show: false, msg: "" });
const [movies, setMovies] = useState([]);
const [query, setQuery] = useState("spider-man");
const fetchMovies = async (url) => {
setIsLoading(true);
try {
const response = await fetch(url);
const data = await response.json();
if (data.Response === "True") {
setMovies(data.Search);
setError({ show: false, msg: "" });
} else {
setError({ show: true, msg: data.Error });
}
setIsLoading(false); // <--- added this bit
} catch (error) {
console.log(error);
setIsLoading(false); // <--- added this bit
}
};
useEffect(() => {
fetchMovies(`${API_ENDPOINT}&s=${query}`);
}, []);
return (
<AppContext.Provider
value={{ isLoading, isError, movies, query, setQuery }}
>
{children}
</AppContext.Provider>
);
};
添加回答
舉報(bào)