简体   繁体   English

开发本机应用程序的正确方法

[英]The correct way of developing a react-native app

I am a react-native newbie. 我是一名反应型新手。 I am wondering if there is a "correct" way to develop a react-native app? 我想知道是否有“正确”的方式来开发本机应用程序? As the documentation is still very insufficient, I am afraid I may develop it in the very incorrect manner, and I prefer correct the mistake now, rather than after the project expands. 由于文档仍然非常不足,因此恐怕我可能会以非常不正确的方式开发它,并且我宁愿现在纠正错误,而不是在项目扩展之后。 From what I understand from my previous experience, we should not combine all pages in a single .js file, but how can each component communicate with each other? 根据我以前的经验,我们不应该将所有页面组合到一个.js文件中,而是每个组件如何相互通信?

I am currently doing like this inside index.android.js: 我目前在index.android.js中这样做:

import Login from './Login';
import Register from './Register';
import Home from './Home';

class TheProject extends Component {
    renderScene (route, navigator) {
        _navigator = navigator;
        switch (route.index) {
            case 'Login':
                return (
                    <View style={styles.container}>
                        <Login navigator={navigator} />
                    </View>
                );
            case 'Register':
                return (
                    <View style={styles.container}>
                        <Register navigator={navigator} />
                    </View>
                );
        }
    }
    render() {
        return (
            <Navigator
                initialRoute={{index: 'Login'}}
                renderScene={this.renderScene}
            />
        );
    }
}

var styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'space-around',
        backgroundColor: '#F0F0F0',
        flexWrap:'wrap'
    },
});

AppRegistry.registerComponent('TheProject', function() { return TheProject });
module.exports = TheProject;

and in my Login.js (other.js file will be similar): 并在我的Login.js中(other.js文件将与此类似):

export default class Login extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            username: '',
            password: '',
        };
    }
    registerOnPress() {
        this.props.navigator.push({
            index: 'Register'
        });
    }
    loginOnPress() {
        if (this.state.username != '' &&  this.state.password != '') {
            Alert.alert('Success!', 'Successfully logged in.');
            this.props.navigator.push({
                index: 'Home'
            });
        } else {
            Alert.alert('Failed to log-in', 'Wrong username or password');
        }
    }
    render () {
        return (
            <View style={styles.individualContainer}>
                <View style={styles.content}>
                    <View style={styles.formField}>
                        <View style={styles.input}>
                            <Text style={styles.label}>Username : </Text>
                            <View style={styles.fieldBox}>
                                <TextInput 
                                    style={styles.field}
                                    underlineColorAndroid={'transparent'}
                                    onChangeText={(data) => this.setState({ username: data })}
                                />
                            </View>
                        </View>
                        <View style={styles.input}>
                            <Text style={styles.label}>Password : </Text>
                            <View style={styles.fieldBox}>
                                <TextInput 
                                    style={styles.field}
                                    underlineColorAndroid={'transparent'}
                                    secureTextEntry={true}
                                    onChangeText={(data) => this.setState({ password: data })}
                                />
                            </View>
                        </View>
                    </View>
                    <View style={styles.input}>
                        <TouchableHighlight style={styles.buttonBox} onPress={this.loginOnPress.bind(this)}>
                            <Text style={styles.buttonText}>Login</Text>
                        </TouchableHighlight>
                    </View>
                    <View style={styles.input}>
                        <TouchableHighlight style={styles.buttonBox} onPress={this.registerOnPress.bind(this)}>
                            <Text style={styles.buttonText}>Register</Text>
                        </TouchableHighlight>
                    </View>
                </View>
            </View>
        );
    }
}

I know there are a lot of ways to develop, but am I on the right track? 我知道有很多开发方法,但是我走对了吗? I am quite blurred when it comes to component mount and unmount. 当涉及到组件安装和卸载时,我很模糊。

Your question is very vague, phrased in such a way that the only real answer is "there is no single right answer". 您的问题非常模糊,用这样一种措辞来表达,即真正的答案是“没有一个正确的答案”。

I suggest looking at the Awesome React Native - Seeds list for good starter kits and the Examples list as well. 我建议您查看Awesome React Native-Seeds列表以获得良好的入门工具包和Examples列表。

Communication between components is quite straight-forwarded and is covered by Facebook's documentation on the subject . 组件之间的通信非常简单, Facebook的主题文档对此进行了介绍。

You should definitely not develop your entire application in a single file. 您绝对不应在单个文件中开发整个应用程序。

Keep in mind that you do not need "React native documentation" to write react native at the beginning. 请记住,您不需要“ React本机文档”即可在一开始就编写React Native。 First you should understand the fundamentals of React because once you do, you'll notice that those fundamentals apply identically whether you're writing a web or native app, the only thing that differs are the components used. 首先,您应该了解React的基础知识,因为一旦这样做,您会发现无论编写Web应用程序还是本机应用程序,这些基础知识都适用,唯一不同的就是所使用的组件。

At first I was about to vote for closing this question, but it's somehow fascinating and tech-related if we just look through the right glasses :) 刚开始时我打算投票赞成结束这个问题,但是如果我们只看一眼正确的眼镜,那一定会令人着迷并且与技术有关:)

  1. At first, you should note you have a lot of nested <View> components. 首先,您应该注意您有很多嵌套的<View>组件。 This could be re-structured, where both username and password label + textinput would be applied as a component - receiving text label and onChangeText as props. 这可以重新构造,其中用户名和密码标签+文本输入都将作为组件应用-接收文本标签和onChangeText作为道具。

  2. Same goes with Login and Register buttons, create them as separate component, where you define title of the button and loginOnPress/registerOnPress functions as props. Login和Register按钮也是如此,将它们创建为单独的组件,在其中定义按钮的标题,loginOnPress / registerOnPress函数作为道具。

  3. Remember to use propTypes whenever needed. 请记住在需要时使用propTypes This helps you and other devs to keep on track what happens in different components and how they're related to each other. 这可以帮助您和其他开发人员跟踪不同组件中发生的情况以及它们之间的相互关系。 For instance: Login.propTypes = { navigator: PropType.func.isRequired} 例如: Login.propTypes = { navigator: PropType.func.isRequired}

  4. Use Flow ( http://flowtype.org ) with you project for types and annotations. 在您的项目中使用Flow( http://flowtype.org )来获取类型和注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM