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

标签 javascript ios reactjs react-native expo

注意:我是 React Native 的新手,搜索了如何执行此操作,但没有找到有用的结果我正在使用 React Native 创建应用程序,并想添加多个组件,例如文本、按钮和文本输入空间,但在没有收到错误的情况下无法做到这一点。有什么方法可以使用 React Native 将多个组件包含到一个 javascript 文档中?

我目前的代码:

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',
  },

});

我想为文本输入添加的代码:

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>
    );
  }
}

我想为按钮添加的代码:

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

最佳答案

您可以在同一文档中创建多个组件,但只能导出默认值一个。

所以你可以像下面这样创建多个组件:

export class UselessTextInput {}

export class UselessTextInputMultiline {}

export class Button {}

访问时:

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

如果你仍然想要单个export default那么:

export default class UselessTextInputMultiline {}

导入时

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

对于,导出多个组件:

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

导入会像:

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

关于javascript - 如何在 React Native 中添加多个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45140368/

相关文章:

javascript - React Native隐藏导航栏元素

javascript - 每 X 秒显示一次警报框,但先等待 Y 秒

ios - 如果没有更多上下文,表达式类型不明确 - FaveButton Swift 4.2 升级

objective-c - 如何将 UIView 放在键盘上方

ios - Xcode 6.3.1 "A signed resource has been added, modified, or deleted"问题

reactjs - 带有嵌套组件的 Jest 快照

javascript - 为什么即使参数作为命名参数传递,参数的顺序仍然保持不变?在 JavaScript 中

javascript - 谷歌地图热图层点半径

javascript - 获取li项的state最大值

javascript - 如何在 TypeScript 中将函数类型声明为参数