注意:我是 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/