Importing Text from local json file in React native

后端 未结 3 1499
不思量自难忘°
不思量自难忘° 2021-01-05 10:39

I am wondering how the common way is to import a huge text to a View. In my case i am developing a React Native App for Android and iOS, and on one of my Views i want to pre

相关标签:
3条回答
  • 2021-01-05 10:58

    You can use:

    import file from './config/TermsAndCondition.json';
    

    then you can treat it as a javascript object:

    file.description
    
    0 讨论(0)
  • 2021-01-05 11:02

    I'd to it as JSON, especially if you need to bring some other (meta)data with the text. Where fetch(url) is called on componentWillMount and <Text>{this.state.hugeText}</Text> component is populated:

    As a concept:

    constructor(props) {
      super(props);
      this.state = {
        hugeText: ''
      };
    }
    
    componentWillMount() {
      fetch('myText.json')
      .then((res) => res.json())
      .then((data) => {
        this.setState({
          hugeText: data.something
        });
      });
    }
    
    0 讨论(0)
  • 2021-01-05 11:06

    I tried fetch but it didn't work (wasn't able to find the file).

    I tried this instead, and it worked:

    // at top of file
    var jsonData = require('./myfile.json');
    
    
    // inside component definition
    render() {
        let deepProp = jsonData.whatever;
    }
    
    0 讨论(0)
提交回复
热议问题