【问题标题】:TypeError: Failed to construct 'Text' ReactNativeTypeError:无法构造“文本”ReactNative
【发布时间】:2022-01-23 23:33:33
【问题描述】:

我已经使用<Text> 标签一百万次并且从未收到此错误,但突然之间,我将<Text> 标签放入<Modal> 标签内,整个世界都在燃烧。我正在使用的组件实际上并没有什么复杂的地方,但是 '' 中的任何 <Text> 都会破坏一切。这是我的代码的样子...

import React, { useState, useEffect } from 'react';
import { View, Modal, Button, ScrollView, TextInput } from 'react-native'
import { settings } from "../../../Styles/SettingStyles";
import { editUserSettings } from '../../../Styles/SettingStyles';

const UpdateUserButton = (props) => {

    const [userInfoChanges, setUserInfoChanges] = useState({})

    const handleInput = (attr, input) => {
        const updateObject = {...userInfoChanges}
        updateObject[attr] = input
        setUserData(updateObject)
    }


    const [modalVisible, setModalVisible] = useState(false)
    return(
        <View style={editUserSettings.editModal}>
            <Modal animationType='slide' transparent={true} visible={modalVisible}>
                <ScrollView style={editUserSettings.container}>
                    <Text style={editUserSettings.editFields}>First Name: </Text>
                    <TextInput
                        name='firstname'
                        style={editUserSettings.input} 
                        onChangeText={(firstname) => {
                            handleInput('firstname', firstname)
                        }}
                    />
                </ScrollView>
            </Modal>
            <Button
                onPress={() => setModalVisible(!modalVisible)}
                title="Edit User Information"
                color="#516ca6"
                accessibilityLabel="Edit User Information"
            />
        </View>
    )
}

export default UpdateUserButton

这个组件的使用方式,它唯一可见的部分将是

<Button
      onPress={() => setModalVisible(!modalVisible)}
      title="Edit User Information"
      color="#516ca6"
      accessibilityLabel="Edit User Information"
    />

当我在浏览器中按下这个按钮时,屏幕全部变成白色,给我一堆错误,都涉及&lt;Text&gt;,第一个看起来像这样......

Uncaught TypeError: Failed to construct 'Text': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

但这对我来说完全没有意义。有人有什么想法吗?

【问题讨论】:

  • 您尚未导入任何名为 Text 的内容。我猜你的全局上下文中有一些符号 Text 是指它。
  • 我讨厌我自己,我以为我检查过了,呃。谢谢!!!

标签: javascript react-native tags


【解决方案1】:

正如@windowsill 在评论中提到的,文本不是从 react-native(或其他任何地方)导入的:)

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多