0

I'm trying to read a text file from my local filesystem using react-native-fs.

However, it keeps telling me the file does not exist (It does, I double checked it and tried to copy the into the run window).

'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);
2

1 Answer 1

4

You should bundle your files into app or put them inside Simulator or Device manually. When you run application it could explore only it's own filesystem, and it can't explre your Windows FS. Use this constants https://github.com/itinance/react-native-fs#constants to get access to files.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.