【发布时间】:2021-05-07 23:36:20
【问题描述】:
我将const 导出到一个文件useLocation.tsx 中,在该文件中获取用户的位置并检索用户的县、州/省和国家/地区。我还在另一个文件useCountryData.tsx 中有一个导出的const,我在其中从API 获取COVID 病例和死亡病例。 useLocation.tsx 中有一个名为 countryNameshort 的变量。如何在useCountryData.tsx 中使用这个变量?
useLocation.tsx
export const useLocation = () => {
var [stateName, setstateName] = useState(String);
var [countyName, setCountyName] = useState(String);
var [countryName, setCountryName] = useState(String);
var [stateNameshort, setstateNameshort] = useState(String);
var [countryNameshort, setCountryNameshort] = useState(String);
const [latitude, setlatitude] = useState(Number);
const [longitude, setlongitude] = useState(Number);
const [location, setLocation] = useState(Object);
const [errorMsg, setErrorMsg] = useState(String);
useEffect(() => {
(async () => {
if (Platform.OS === "android" && !Constants.isDevice) {
setErrorMsg(
"Oops, this will not work on Snack in an Android emulator. Try it on your device!"
);
return;
}
let { status } = await Location.requestPermissionsAsync();
if (status !== "granted") {
setErrorMsg("Permission to access location was denied");
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
const latitude = location.coords.latitude;
setlatitude(latitude);
const longitude = location.coords.longitude;
setlongitude(longitude);
})();
}, []);
let text = "Waiting..";
if (errorMsg) {
text = errorMsg;
} else if (location) {
text = JSON.stringify(location);
}
fetch(
"https://maps.googleapis.com/maps/api/geocode/json?address=" +
latitude +
"," +
longitude +
"&key=" +
apiKey
)
.then((response) => response.json())
.then((responseJson) => {
const resState = responseJson.results[0].address_components.filter(
(x: any) =>
x.types.filter((t: Object) => t == "administrative_area_level_1")
.length > 0
)[0].long_name;
setstateName(resState);
const resCounty = responseJson.results[0].address_components.filter(
(x: any) =>
x.types.filter((t: Object) => t == "administrative_area_level_2")
.length > 0
)[0].long_name;
setCountyName(resCounty);
const resCountry = responseJson.results[0].address_components.filter(
(x: any) => x.types.filter((t: Object) => t == "country").length > 0
)[0].long_name;
setCountryName(resCountry);
const resStateShort = responseJson.results[0].address_components.filter(
(x: any) =>
x.types.filter((t: Object) => t == "administrative_area_level_1")
.length > 0
)[0].short_name;
setstateNameshort(resStateShort);
const resCountryShort = responseJson.results[0].address_components.filter(
(x: any) => x.types.filter((t: Object) => t == "country").length > 0
)[0].short_name;
setCountryNameshort(resCountryShort);
if (countryNameshort === "US") {
countryNameshort = "US" + "A";
}
})
.catch((err) => {
console.log(err);
});
return { countryName, countyName, stateName, stateNameshort, countryNameshort };
};
useCountryData.tsx
import { useLocation } from './useLocation';
export const useCountryData = () => {
const [earliest2, setEarliest2] = useState([]);
const [countryDeaths, setcountryDeaths] = useState(Number);
const [countryCases, setcountryCases] = useState(Number);
useEffect(() => {
axios
.get("https://coronavirus-19-api.herokuapp.com/countries")
.then((response) => {
setEarliest2(response.data);
const countryArray = response.data.filter(
(item) => item.country === props.countryNameshort //???
);
const resCountryDeaths = countryArray[0].deaths;
setcountryDeaths(resCountryDeaths);
const resCountryCases = countryArray[0].cases;
setcountryCases(resCountryCases);
console.log("hiiii", countryCases);
})
.catch((err) => {
console.log(err);
});
}, []);
return { countryCases, countryDeaths };
};
CountryCard.tsx
const CountryCard = (props) => {
const mappedLocation = useMappedLocation();
const countryName = mappedLocation.country;
return (
<RectButton style={[styles.container, { backgroundColor: "white" }]}>
<Text style={[styles.textLocation, { top: 15, left: 10 }]}>
{countryName} /???
</Text>
)
}
【问题讨论】:
-
您不需要使用
.tsx,因为您不使用 tsx 表达式(在您的打字稿/javascript 中使用“html 代码”)。一般来说,如果你想使用导出的常量,你需要将它们导入你的文件import { myConst } from 'myFile' -
当你没有进行任何实际的 React DOM 操作时,这看起来就像你使用了 React 代码和模式。因此,一切都比它需要的更加混乱。您正在执行的任务是简单的异步操作,可能导致分配作为承诺的 const 值。然后,这些承诺值可以导出并等待,无论您关心在最终解决该特定值时使用该值。了解 await 和 async 并努力删除代码中对 useEffect 和 useState 的所有引用。它们是多余的并造成了问题。
-
@messerbill 我实际上正在使用 react-native typescript 进行移动开发。我不明白你不使用
.tsx.的意思我已经在导入常量,但我不知道我是否正确传递了countryNameshort。我已经更新了上面的代码。 -
@cefn 我需要使用 React DOM 进行移动开发吗?我只是想知道如何在
useLocation.tsx和useCountryData.tsx中使用countryNameshort,也许使用React 道具。 -
我建议阅读更多关于
javascript和typescript之间的区别以及何时使用jsx或tsx。 reactjs.org/docs/introducing-jsx.html干杯
标签: javascript typescript react-native constants