第一次使用 Context API,请耐心等待。
我有一个本地 JSON 文件,我试图从中获取响应并将响应放入 Context API 中,以便我可以在应用程序中全局使用。
在本地运行应用程序时,出现空白屏幕,并且页面似乎无法加载。该页面处于无限循环中,因此我看不到它是否正在加载。
代码沙箱在这里。
知道我可以更改什么才能将数据响应成功引入 Context API?
apiContext.js:
import React, { useContext, useState, useEffect, createContext } from "react";
import axios from "axios";
const APIContext = createContext();
function APIContextProvider({ children }) {
// Initialize state
const [data, setData] = useState([]);
const [isLoading, setIsLoading] = useState(true);
// Fetch data
useEffect(() => {
let url = "../db.json";
axios
.get(url)
.then(function (response) {
setData(response.data.machines);
setIsLoading(false);
console.log(response.data.machines);
})
.catch((error) => console.log(error));
}, []);
return (
<APIContextProvider …Run Code Online (Sandbox Code Playgroud) 第一次尝试这个所以请和我一起裸露。
我想根据三个条件之一返回一些元素。目前该代码适用于两种情况:
<h1 id="largeh1">
{isEnvironmentBFE
? outputEnFr(
"BFE Environment English Text",
"BFE Environment French Text",
this.props.lang
)
: outputEnFr(
"Acme Environment English Text",
"Acme Environment French Text",
this.props.lang
)
// Want third condition here
}
</h1>
Run Code Online (Sandbox Code Playgroud)
我想为环境是否为“isEnvironmentBFE_OR_BFERENTAL”添加一个条件 - 因此当人们遇到第三个环境时,它会给他们不同的英语/法语值。如果可能的话,想坚持使用三元运算符。
知道如何在这里添加第三个条件吗?
我正在构建一个融资计算器,所有输出到 DOM 的数字都以数千计。
我目前在我的代码中的一个数字上使用 .toLocaleString() ,并且它有效(主要的 productPrice 数字)。我在输出到 DOM 时使用了 .toLocatelString()。
但是,我似乎无法弄清楚为什么在使用相同的方式时,它不适用于其他数字。具体来说,首付、总额和每月数字。
这是 JS 代码(我输入的代码 .toLocaleString() 在最底部):
"use strict";
// Define product price / tax
const productPrice = 105000;
const tax = 0.13;
// Append product price to DOM
const productPriceID = document.getElementById("product-price");
productPriceID.innerHTML = productPrice.toLocaleString();
// Grab the id's of the main product price, down payment, total, per month and button for DOM appending
const downPaymentValue = document.getElementById("down-payment-value");
const totalValue = document.getElementById("total-value");
const perMonthValue = document.getElementById("per-month-value");
const calculateBtn = …Run Code Online (Sandbox Code Playgroud)