![](/img/trans.png)
[英]How can I fetch JSON from an URL and pass it to a const in react?
[英]How can i fetch a value from a const using a key from another js class n react-native?
我可以使用同一類中const的鍵獲取數據,但是當將const放置在另一個js文件中時如何實現相同的結果呢? 我將鍵發送為“ 1”時需要一個值“ Afganistan”。
CountryCode.js file:
const CountryCode = {
'1': 'Afghanistan',
'2': 'Albania',
'3': 'Algeria',
'4': 'American Samoa',
'5': 'Andorra'
};
HomePage.js file:
import CountryCode from'./CountryCode';
render() {
return (
<View style={styles.MainContainer}>
<Text style={styles.listCountry}>{CountryCode['1']}</Text>
</View>
);
}
在export default CountryCode
的末尾添加export default CountryCode
的CountryCode
const CountryCode = {
'1': 'Afghanistan',
'2': 'Albania',
'3': 'Algeria',
'4': 'American Samoa',
'5': 'Andorra'
}
export default CountryCode
現在,該變量應該在另一個文件中具有該值。
首先,您必須從CountryCode.js文件中導出數據(數組),如下所示。
CountryCode.js file:
export default {
'1': 'Afghanistan',
'2': 'Albania',
'3': 'Algeria',
'4': 'American Samoa',
'5': 'Andorra'
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.