React-native 학습 과정 3 네트워크를 통해 데이터 얻기

이번에는 어떻게 인터넷에서 데이터를 얻는지, 그리고 먼저 대상의 URL을 맨 위에 놓는지 보여 준다.
var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';

데이터 초기화
constructor(props) {
    super(props);
    this.state = {
      movies: null,
    };
}

데이터를 얻습니다. 이것은 componentDidMount입니다. 즉, 초기화가 끝난 후에 한 번 실행됩니다.
componentDidMount() {
    this.fetchData();
  }
fetchData() {
    fetch(REQUEST_URL)
      .then((response) => response.json())
      .then((responseData) => {
        this.setState({
          movies: responseData.movies,
        });
      })
      .done();
  }

데이터를 성공적으로 얻었는지 판단하여 상황에 따라 레이아웃을 표시합니다
render() {
    if (!this.state.movies) {
      return this.renderLoadingView();
    }

    var movie = this.state.movies[0];
    return this.renderMovie(movie);
  }
  renderLoadingView() {
    return (
      <View style={styles.container}>
        <Text>
          Loading movies...
        Text>
      View>
    );
  }

  renderMovie(movie) {
    return (
      <View style={styles.container}>
        <Image
          source={{uri: movie.posters.thumbnail}}
          style={styles.thumbnail}
        />
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{movie.title}Text>
          <Text style={styles.year}>{movie.year}Text>
        View>
      View>
    );
  }

전체 코드는 다음과 같다.
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
var MOCKED_MOVIES_DATA = [
  {title: 'Title', year: '2015', posters: {thumbnail: 'http://img5.imgtn.bdimg.com/it/u=4080105893,4096129906&fm=206&gp=0.jpg/'}},
];
var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';
import React, { Component } from 'react';
import {
  AppRegistry,
  Image,
  StyleSheet,
  Text,
  View,
   ListView,
} from 'react-native';

export default class MyProject extends Component {
     constructor(props) {
         super(props);
         this.state = {
         movies: null,
       };
     }

     componentDidMount(){
         this.fetchData();
     }

     fetchData() {
         fetch(REQUEST_URL)
             .then((response) => response.json())
             .then((responseData) => {
                 this.setState({
                     movies: responseData.movies,
                 });
             })
             .done();
     }

     render() {
         if (!this.state.movies) {
             return this.renderLoadingView();
         }

         var movie = this.state.movies[0];
          return this.renderMovie(movie);
     }

     renderLoadingView()
     {
         return (
            
               
                   Loading movies...
               
            
        );
     }

     renderMovie(movie) {
         return (
             
                 
                 
                     {movie.title}
                     {movie.year}
                 
             
         );
     }


}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
    thumbnail: {
    width: 53,
    height: 81,
    marginLeft:30,
  },
    rightContainer: {
    flex: 1,
  },
  title: {
    fontSize: 20,
    marginBottom: 8,
    textAlign: 'center',
  },
  year: {
    textAlign: 'center',
  },
       listView: {
         paddingTop: 20,
         backgroundColor: '#F5FCFF',
     },

});

AppRegistry.registerComponent('MyProject', () => MyProject);

안녕히 계십시오.

좋은 웹페이지 즐겨찾기