简单获取 api 示例不起作用

simple fetch api example not working

我是 react-native 的新手,这是我的代码:

import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View
} from 'react-native';

class FetchExample extends Component {
    constructor() {
        super();
        this.state = {
            date: ''
        }
    }
    render() {
        return (
            <View style={styles.container}>
                <Text>{}</Text>
            </View>
        );
    }
    componentWillMount() {
        fetch("http://date.jsontest.com/")
            .then((response) => response.json())
            .then((responseData) => {
                console.log('a');
                this.setState({date: responseData.date});
            })
            .done();
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    }
});

AppRegistry.registerComponent('ak', () => FetchExample);

这是日志错误:

10:53:38 PM: Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in. Check the render method of AwakeInDevApp. in AwakeInDevApp (at registerRootComponent.js:36) in RootErrorBoundary (at registerRootComponent.js:35) in ExpoRootComponent (at renderApplication.js:35) in RCTView (at View.js:113) in View (at AppContainer.js:102) in RCTView (at View.js:113) in View (at AppContainer.js:126) in AppContainer (at renderApplication.js:34) - node_modules/fbjs/lib/warning.js:33:20 in printWarning - ... 24 more stack frames from framework internals

您需要导出组件。像这样

export default class FetchExample extends Component {