前端技术前端知识

【React Native 极速指南】基础篇

2019-04-26  本文已影响35人  一俢

这篇文章你将会学习到:

如何安装 React Native

ABC

Hello World

// hello-world.js
import React, { Component } from 'react';
import { Text, View } from 'react-native';

const styles = {
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    }
};

export default class HelloWorld extends Component {
    render() {
        return (
            <View style={styles.container}>
                <Text>
                    Hello World!
                </Text>
            </View>
        );
    }
}

属性 Props

// my-component.js
import React, { Component } from 'react';
import { Text } from 'react-native';

export default class MyComponent extends Component {

    render() {
        return (
            <Text>Hello {this.props.text}</Text>
        )
    }
}
// hello-world.js
//...
import MyComponent from './my-component';

//...

export default class HelloWorld extends Component {
    render() {
        return (
            <View style={styles.container}>
                <Text>
                    Hello World!
                </Text>
                <MyComponent text="My Component"></MyComponent>
            </View>
        );
    }
}

状态 State

// my-state.js
import React, { Component } from 'react';
import { Text, View } from 'react-native';

export default class MyState extends Component {

    state = {
        message: '',
    };

    componentDidMount() {
        setTimeout(() => {
            this.setState({
                message: 'Hello State!'
            });
        }, 1000)
    }

    render() {
        return (
            <View>
                <Text>
                    {this.state.message}
                </Text>
            </View>
        );
    }
}

样式 Style

弹性布局 Flex

事件处理 Text Input

// my-input.js
import React, { Component } from 'react';
import { Text, View, TextInput } from 'react-native';

export default class MyInput extends Component {

    state = {
        text: '',
    };

    render() {
        return (
            <View>
                <TextInput onChangeText={(text) => this.setState({ text })}></TextInput>
                <Text>{this.state.text}</Text>
            </View>
        );
    }
}


组件 ScrollView

// my-scroll.js
import React, { Component } from 'react';
import { Text, ScrollView } from 'react-native';

export default class MyScroll extends Component {

    render() {
        return (
            <ScrollView>
                <Text style={{ fontSize: 96 }}>Large Word</Text>
                <Text style={{ fontSize: 96 }}>Big World</Text>
                <Text style={{ fontSize: 96 }}>Great World</Text>
            </ScrollView>
        );
    }
}

组件 FlatList and SectionList

// my-flat-list.js
import React, { Component } from 'react';
import { FlatList, StyleSheet, Text } from 'react-native';

export default class MyFlatList extends Component {
    render() {
        return (
            <FlatList
                data={[
                    { key: 'Devin' },
                    { key: 'Jackson' },
                    { key: 'James' },
                    { key: 'Joel' },
                    { key: 'John' },
                    { key: 'Jillian' },
                    { key: 'Jimmy' },
                    { key: 'Julie' },
                ]}
                renderItem={({ item }) => <Text>{item.key}</Text>}
            />
        );
    }
}

网络 Network

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

export default class MyNetwork extends Component {

    state = {
        data: ''
    };

    async componentDidMount() {
        const res = await fetch('http://ec2-54-222-217-237.cn-north-1.compute.amazonaws.com.cn:12800/api/v0/cat/Qmaj8UWNjTzBMBHkkaqSiyax2nFgiwYP2ewxnhGBucn6S8')
        const json = await res.json();
        console.log(json);
        this.setState({data: json})
    }

    render() {
        return (
            <View>
                <Text></Text>
            </View>
        );
    }
}

〖坚持的一俢〗

上一篇 下一篇

猜你喜欢

热点阅读