P粉9044059412023-08-18 11:26:59
I'm not sure, but this might help you.
const [loading, setLoading] = useState(true); const [data1, setData1] = useState(null); const [percentageDone, setPercentageDone] = useState(0); const loadDataFunction = async () => { // 执行 API 调用并更新 percentageDone }; useEffect(() => { loadDataFunction().then(() => { setLoading(false); }); }, []); useEffect(() => { if (condition) { setLoading(true); loadDataFunction().then(() => { setLoading(false); }); } }, [condition]); return ( loading ? ( <div>{percentageDone}% 加载中...</div> ) : ( <RenderData1Component data={data1} /> ) );
Possibly you are giving multiple conditions, which may cause problems. Please check this out too.