useeffect関係の機能最適化

This commit is contained in:
harukin-expo-dev-env 2025-08-03 08:22:51 +00:00
parent c0804d2ac7
commit a2a6c7fdb9
5 changed files with 25 additions and 15 deletions

4
App.js
View File

@ -32,7 +32,9 @@ if (Platform.OS === "android") {
} }
export default function App() { export default function App() {
useEffect(() => UpdateAsync(), []); useEffect(() => {
UpdateAsync();
}, []);
const ProviderTree = buildProvidersTree([ const ProviderTree = buildProvidersTree([
AllTrainDiagramProvider, AllTrainDiagramProvider,

View File

@ -16,11 +16,13 @@ export const AllTrainDiagramProvider = ({ children }) => {
const [allTrainDiagram, setAllTrainDiagram] = useState(trainList); const [allTrainDiagram, setAllTrainDiagram] = useState(trainList);
const [allCustonTrainData, setAllCustonTrainData] = useState([]); // カスタム列車データ const [allCustonTrainData, setAllCustonTrainData] = useState([]); // カスタム列車データ
const [keyList, setKeyList] = useState(); // 第二要素 const [keyList, setKeyList] = useState(); // 第二要素
useEffect( useEffect(() => {
() => allTrainDiagram && setKeyList(Object.keys(allTrainDiagram)), if (allTrainDiagram && Object.keys(allTrainDiagram).length > 0)
[allTrainDiagram] setKeyList(Object.keys(allTrainDiagram));
); else setKeyList([]);
const getTrainDiagram = () => fetch("https://n8n.haruk.in/webhook/JR-shikoku-diagram-migrate-original") }, [allTrainDiagram]);
const getTrainDiagram = () =>
fetch("https://n8n.haruk.in/webhook/JR-shikoku-diagram-migrate-original")
.then((res) => res.json()) .then((res) => res.json())
.then((res) => res.data) .then((res) => res.data)
.then((res) => { .then((res) => {
@ -42,12 +44,13 @@ export const AllTrainDiagramProvider = ({ children }) => {
alert("初回の路線情報の取得に失敗しました。"); alert("初回の路線情報の取得に失敗しました。");
}); });
}); });
useEffect(getTrainDiagram, []); useEffect(() => {
getTrainDiagram();
}, []);
useInterval(getTrainDiagram, 30000); //30秒毎に全在線列車取得 useInterval(getTrainDiagram, 30000); //30秒毎に全在線列車取得
const getCustomTrainData = () => {
const getCustomTrainData = ()=>{
fetch("https://n8n.haruk.in/webhook/jr-shikoku-position-custom-datalist") fetch("https://n8n.haruk.in/webhook/jr-shikoku-position-custom-datalist")
.then((res) => res.json()) .then((res) => res.json())
.then((res) => { .then((res) => {
@ -56,8 +59,7 @@ export const AllTrainDiagramProvider = ({ children }) => {
.catch(() => { .catch(() => {
alert("カスタム列車データの取得に失敗しました。"); alert("カスタム列車データの取得に失敗しました。");
}); });
} };
useEffect(() => { useEffect(() => {
// カスタム列車データの取得 // カスタム列車データの取得

View File

@ -378,7 +378,9 @@ export const AreaInfoProvider = ({ children }) => {
); );
}); });
}; };
useEffect(getAreaData, []); useEffect(() => {
getAreaData();
}, []);
useInterval(getAreaData, 60000); //60秒毎に全在線列車取得 useInterval(getAreaData, 60000); //60秒毎に全在線列車取得
return ( return (
<AreaInfoContext.Provider <AreaInfoContext.Provider

View File

@ -71,7 +71,9 @@ export const CurrentTrainProvider = ({ children }) => {
webview.current?.injectJavaScript(i); webview.current?.injectJavaScript(i);
}; };
useEffect(getCurrentTrain, []); //初回だけ現在の全在線列車取得 useEffect(() => {
getCurrentTrain();
}, []); //初回だけ現在の全在線列車取得
useInterval(getCurrentTrain, 15000); //15秒毎に全在線列車取得 useInterval(getCurrentTrain, 15000); //15秒毎に全在線列車取得

View File

@ -58,7 +58,9 @@ export const UserPositionProvider: FC<Props> = ({ children }) => {
if (Platform.OS == "web") return; if (Platform.OS == "web") return;
getLocationPermission(); getLocationPermission();
}, []); }, []);
useEffect(getCurrentPosition, [locationStatus]); useEffect(() => {
getCurrentPosition();
}, [locationStatus]);
useInterval(getCurrentPosition, 5000); useInterval(getCurrentPosition, 5000);
return ( return (