react native에서 로컬 JSON 파일에서 데이터를 가져오려면 어떻게 해야 합니까?
JSON 등의 로컬 파일을 저장하고 컨트롤러에서 데이터를 가져오려면 어떻게 해야 합니까?
React Native 0.4.3에서는 다음과 같이 로컬 JSON 파일을 읽을 수 있습니다.
const customData = require('./customData.json');
그런 다음 일반 JS 개체처럼 customData에 액세스합니다.
ES6/ES2015 버전:
import customData from './customData.json';
ES6/ES2015의 경우 다음과 같이 직접 가져올 수 있습니다.
// example.json
{
"name": "testing"
}
// ES6/ES2015
// app.js
import * as data from './example.json';
const word = data.name;
console.log(word); // output 'testing'
typescript를 사용하는 경우 다음과 같이 json 모듈을 선언할 수 있습니다.
// tying.d.ts
declare module "*.json" {
const value: any;
export default value;
}
이것을 사용하다
import data from './customData.json';
로컬 JSON 파일을 가져오는 방법은 다음과 같습니다.
ES6 버전:
import customData from './customData.json';또는import customData from './customData';
파일이 파일이 아닌 내부 파일인 경우 다음과 같이 Import합니다.
import { customData } from './customData';
자세한 설명/이해는 예시를 참조하십시오.실시간 작업 데모
AsyncStorage setItem과 getItem을 사용할 수 있습니다.데이터를 문자열로 저장하고 json 파서를 사용하여 다시 json으로 변환합니다.
이 Github 문제를 보세요.
https://github.com/facebook/react-native/issues/231
그들은 하려고 한다.require비 JSON 파일, 특히 JSON 파일.현재로서는 방법이 없기 때문에 @CocoOS에서 설명한 바와 같이 AsyncStorage를 사용하거나 필요한 작업을 수행하기 위해 작은 네이티브 모듈을 작성할 수 있습니다.
언급URL : https://stackoverflow.com/questions/29452822/how-to-fetch-data-from-local-json-file-on-react-native
'source' 카테고리의 다른 글
| WP REST API 기본 경로 사용 안 함 (0) | 2023.03.29 |
|---|---|
| 구성 요소 상태의 어레이에서 요소를 제거하는 중 (0) | 2023.03.19 |
| Wordpress localhost ftp (0) | 2023.03.19 |
| SQL 최신 날짜 항목 선택 방법 (0) | 2023.03.19 |
| 텍스트만 있는 단락 사이에 광고를 배치하다 (0) | 2023.03.19 |