React Native :文本字符串必须在组件内呈现< Text>

webghufk  于 2022-11-25  发布在  React
关注(0)|答案(5)|浏览(140)

我正在尝试创建一个个人资料页面,用户可以在其中上传图像作为react-native-elements Avatar,并在native-base form element上更新其个人资料信息。
我还使用React Native默认ImageEditor进行图像裁剪,并使用Expo中的ImagePicker选择图像。
但当我打开Expo上的应用程序时,我收到此错误

不变违例:固定违规:文本字符串必须在组件内呈现

下面是我正在使用的代码。
请帮帮忙。

import React from "react";
    import {
      View,
      Text,
      FlatList,
      ActivityIndicator,
      TouchableOpacity,
      StyleSheet,
     ImageEditor
    } from "react-native";
    import { Avatar } from "react-native-elements";
    import { Container, Content, Form, Input, Label, Item } from 'native-base';
    import * as Expo from 'expo';

    export default class ProfileScreen extends React.Component {
     static navigationOptions = {

     }

     constructor(props) {
     super(props);

     this.state = {
      loading: false,
      image: null,
      error: null,
      refreshing: false
     };
    }

    async _pickImage() {
    let result = await Expo.ImagePicker.launchImageLibraryAsync({
      allowsEditing: true,
      aspect: [4, 3],
    });

    if (result.cancelled) {
      console.log('got here');
      return;
    }

    let resizedUri = await new Promise((resolve, reject) => {
      ImageEditor.cropImage(result.uri,
        {
          offset: { x: 0, y: 0 },
          size: { width: result.width, height: result.height },
          displaySize: { width: 100, height: 100 },
          resizeMode: 'contain',
        },
        (uri) => resolve(uri),
        () => reject(),
      );
    });

    // this gives you a rct-image-store URI or a base64 image tag that
    // you can use from ImageStore

    this.setState({ image: resizedUri });
    }

    render () {
    let { image } = this.state;
    return (
      <Container>
        <Content>
            <View style={{flex:1, flexDirection: 'column', alignContent: 'flex-start', marginLeft: 20}}>
              <View style={{flex:1, flexDirection: 'row', alignContent: 'flex-end'}}>
                <TouchableOpacity onPress={() => alert('Save')}>
                    <Text style={{color: '#1f618d', fontWeight: 'bold'}}>Save</Text>
                </TouchableOpacity>
                <TouchableOpacity onPress={() => alert('Cancel')}>
                    <Text style={{color: '#1f618d', fontWeight: 'bold'}}>Cancel</Text>
                </TouchableOpacity>
              </View>
              <View style={{height: 30}}></View> //Empty view
              <View style={{alignContent: 'center'}}>
                <Avatar rounded size="xlarge" title="Profile Photo" source={{ uri: this.state.image }} onPress={this._pickImage}/>
              </View>
              <View style={{height: 30}}></View> //Empty view
              <Form>
                <Item floatingLabel>
                  <Label style={styles.labelText}>First Name</Label>
                  <Input/>
                </Item>
                <Item floatingLabel>
                  <Label style={styles.labelText}>Last Name</Label>
                  <Input/>
                </Item>
                <Item floatingLabel>
                  <Label style={styles.labelText}>Email</Label>
                  <Input/>
                </Item>
              </Form>
            </View>
        </Content>
      </Container>
    )
    }
    }

    const styles = StyleSheet.create({
    labelText: {
    fontSize: 12,
    color: '#1f618d',
    fontWeight: '100'
    }
    });
6fe3ivhb

6fe3ivhb1#

问题是在render //Empty View中使用注解的方式使用了类似于{/* Empty view */}的内容

wbrvyc0a

wbrvyc0a2#

JSX内的注解必须具有以下语法。

{/* Empty view */}
xvw2m8pv

xvw2m8pv3#

如果你想在render return方法中添加注解,你必须使用类似{/*Empty View*/}的方法。
<View style={{height: 30}}></View> //Empty view改为<View style={{height: 30}}>{/*Empty View*/}</View>
不能像//comments那样在return函数中直接添加注解,只允许在呈现或业务逻辑部分中添加。
谢谢

2skhul33

2skhul334#

删除//注解
使用jsx注解风格

{/* comment */}
628mspwn

628mspwn5#

如果将空字符串传递到组件中,然后在<Text>元素中“呈现”该组件,则会发生这种情况。

相关问题