32 lines
918 B
JavaScript
32 lines
918 B
JavaScript
import React, { createContext, useContext, useState } from "react";
|
|
const initialState = {
|
|
currentTrain: [],
|
|
setCurrentTrain: () => {},
|
|
currentTrainLoading: "loading",
|
|
setCurrentTrainLoading: () => {},
|
|
};
|
|
|
|
const CurrentTrainContext = createContext(initialState);
|
|
|
|
export const useCurrentTrain = () => {
|
|
return useContext(CurrentTrainContext);
|
|
};
|
|
|
|
export const CurrentTrainProvider = ({ children }) => {
|
|
const [currentTrain, setCurrentTrain] = useState([]); //現在在線中の全列車 { num: 列車番号, delay: 遅延時分(状態), Pos: 位置情報 }
|
|
const [currentTrainLoading, setCurrentTrainLoading] = useState("loading"); // success, error, loading
|
|
|
|
return (
|
|
<CurrentTrainContext.Provider
|
|
value={{
|
|
currentTrain,
|
|
setCurrentTrain,
|
|
currentTrainLoading,
|
|
setCurrentTrainLoading,
|
|
}}
|
|
>
|
|
{children}
|
|
</CurrentTrainContext.Provider>
|
|
);
|
|
};
|