简体   繁体   English

如何在React Native中添加多个组件?

[英]How to add multiple components in React Native?

Note: I am new to React Native and have searched up how to do this but found no helpful results I am using React Native to create an app and want to add multiple components, such as text, buttons, and a text input space, but am having trouble doing so without receiving errors. 注意:我是React Native的新手,已经搜索了如何执行此操作,但是没有发现有用的结果,我正在使用React Native来创建应用程序,并希望添加多个组件,例如文本,按钮和文本输入空间,但是在没有收到错误的情况下遇到麻烦。 Is there any way to include multiple components into one javascript document using React Native? 有什么方法可以使用React Native将多个组件包含到一个javascript文档中吗?

The code I currently have: 我目前拥有的代码:

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

export default class App extends React.Component {
  render() {
    return (
      <View style={{alignItems: 'center'}}>
        <Text style={styles.bigblack}>Sample Bold Text Here</Text>
        <Text>Sample Text Here:</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  bigblack: {
    color: 'black',
    fontWeight: 'bold',
    fontSize: 28,
  },
  red: {
    color: 'red',
  },
  container: {
    flex: 1,
    backgroundColor: '#fdf5e6',
    alignItems: 'center',
    justifyContent: 'center',
  },

});

Code I want to add for Text Input: 我要为文本输入添加的代码:

class UselessTextInput extends Component {
  render() {
    return (
      <TextInput
        {...this.props} 
        editable = {true}
        maxLength = {40}
      />
    );
  }
}

export default class UselessTextInputMultiline extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: 'Useless Multiline Placeholder',
    };
  }

  render() {
    return (
     <View style={{
       backgroundColor: this.state.text,
       borderBottomColor: '#000000',
       borderBottomWidth: 1 }}
     >
       <UselessTextInput
         multiline = {true}
         numberOfLines = {4}
         onChangeText={(text) => this.setState({text})}
         value={this.state.text}
       />
     </View>
    );
  }
}

Code I want to add for Button: 我要为Button添加的代码:

<Button
  onPress={onPressLearnMore}
  title="Learn More"
  color="#841584"
  accessibilityLabel="Learn more about this button"
/>

You can create multiple component in same document but can export default only one. 您可以在同一文档中创建多个组件,但只能export default组件。

So you can create multiple component like below: 因此,您可以创建多个组件,如下所示:

export class UselessTextInput {}

export class UselessTextInputMultiline {}

export class Button {}

while accessing : 在访问时:

import {UselessTextInput, UselessTextInputMultiline, Button} from './components/customInput' // change with your respective path

if you still want to have single export default then: 如果您仍然希望具有单个export default则:

export default class UselessTextInputMultiline {}

and while importing 并且在导入时

import Template,{Button} from './components/customInput'

For, exporting multiple component: 对于,导出多个组件:

module.exports = {
    text: UselessTextInput,
    btn: Button
}

imports will be like: 进口将像:

let txtInput= require('./components/customInput').text;
let btnInput = require('./components/customInput').btn;

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

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