【发布时间】:2017-08-05 07:51:32
【问题描述】:
我正在尝试使用从本地文件系统读取文本文件 react-native-fs.
但是,它一直告诉我该文件不存在(确实如此,我仔细检查了它并尝试将其复制到运行窗口中)。
'use strict';
import React, { Component } from 'react';
//import FileSystem from 'react-native-filesystem';
import RNFS from 'react-native-fs';
import {
AppRegistry,
StyleSheet,
Text,
View,
NetInfo
} from 'react-native';
export default class FactsApiFetcher extends Component
{
constructor(props)
{
super(props);
this.state =
{
fact: 'Loading...'
}
}
componentWillMount()
{
this._getRandomFact();
}
_getRandomFact()
{
const fileContents = RNFS.read("D:\\AllRand\\facts-api-handler\\test.txt") //FileSystem.readFile("D:\\AllRand\\allfacts\\test.txt");
this.setState({
fact: fileContents
})
}
render()
{
return <Text> { this.state.fact } </Text>;
}
}
AppRegistry.registerComponent('FactsApiFetcher', () => FactsApiFetcher);
【问题讨论】:
-
你设置好配置了吗? github.com/itinance/react-native-fs#usage-android,那里也有一个示例用法。
-
从文档github.com/itinance/…你需要在使用方法
read()时返回promise
标签: javascript java reactjs react-native