您好,我有一个上下文提供程序文件,下面_app.js文件。我想访问_app.js文件中的AppContext。任何人都可以帮助我,我如何访问_app.js文件中的AppContext。
我在_app.js文件中导入上下文并检查,但我无法_app.js文件中获取上下文。任何人都可以帮我纠正这个代码,让我知道这里有什么问题。
上下文文件
import React, { createContext, useState } from 'react';
export const AppContext = createContext();
let timeout = null;
const baseURL = "api/v1/";
const headers = { 'Accept': 'application/json', "Content-type": "application/json" };
const AppContextProvider = (props) => {
const [account, setAccount] = useState();
const [alert, setAlert] = useState();
const AlertType = { success: "success", error: "error", info: "info", warning: "warning" }
const updateAccount = (objAccount) => {
setAccount(objAccount);
}
const updateAlert = (objAlert) => {
if (objAlert) {
if (objAlert.type == AlertType.success) { objAlert = { message: objAlert.message, type: objAlert.type, cssClass: "alert-teal" }; }
else if (objAlert.type == AlertType.error) { objAlert = { message: objAlert.message, type: objAlert.type, cssClass: "alert-danger" }; }
else if (objAlert.type == AlertType.warning) { objAlert = { message: objAlert.message, type: objAlert.type, cssClass: "alert-warning" }; }
else if (objAlert.type == AlertType.info) { objAlert = { message: objAlert.message, type: objAlert.type, cssClass: "alert-info" }; }
}
setAlert(objAlert);
clearTimeout(timeout);
timeout = setTimeout(() => { setAlert(); }, 8000);
}
const ExecuteAPI_Get = async (action) => {
let url = baseURL + action;
let response = await fetch(url, {
method: 'GET',
headers: headers
}).catch(err => { console.log(err); });
return await response.json();
}
精慕HU
小唯快跑啊
相关分类