未定义不是对象 [React Native]

发布于 2025-01-09 02:59:54 字数 865 浏览 4 评论 0原文

我在文本输入方面遇到了一些小问题。

我有一个带有 3 个文本输入的屏幕,每个输入都有自己的状态。当我尝试捕获用户输入的值并将其保存在状态中时,会出现问题,但我有“未定义不是 setState 中的对象”。我阅读了各种帖子,并尝试了这些解决方案,但我仍然保留着这个问题。

这里我把我的想法拆下来。

//这里我从另一个屏幕发送的道具中声明状态“Nombre”

const [stateNombre, setStateNombre] = useState(JSON.stringify(param_nombre));

//这里我声明TextInput

<TextInput
        id="text_nombre"
        placeholder={"Nombre"}
        style={styles.data_input}
        autoCorrect={false}
        blurOnSubmit
        placeholderTextColor="#777"
        autoCapitalized="words"
        multiline={true}
        onChangeText={text => this.setStateNombre(text.target.value)}
      ></TextInput>

//这里问题的图片

在此处输入图像描述

I'm with little issues with text inputs.

I have a screen with 3 text inputs, each have his own state. The problem occurs when I try to capture the value enter from the user and save it in a state, but i have "undefined is not an object in the setState". I read various threads, and I tried those solves, but I still keeping that problem.

Here I detach how I thought.

//Here I declare the state "Nombre" from a props sending by another screen

const [stateNombre, setStateNombre] = useState(JSON.stringify(param_nombre));

//Here I declare the TextInput

<TextInput
        id="text_nombre"
        placeholder={"Nombre"}
        style={styles.data_input}
        autoCorrect={false}
        blurOnSubmit
        placeholderTextColor="#777"
        autoCapitalized="words"
        multiline={true}
        onChangeText={text => this.setStateNombre(text.target.value)}
      ></TextInput>

//Here a picture of the problem

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼藏柔 2025-01-16 02:59:54

看来您将类组件和功能组件组合在一起
为什么我说你使用来自功能组件的 useState 和来自类组件 + 功能组件的 this.setStateNombre。它是某种奇怪的代码。

当涉及到您的 Problem First log(JSON.stringify(param_nombre))

和 For TextInput [组件和不受控制的组件]

<TextInput
        value={stateNombre}
        id="text_nombre"           
        placeholder={"Nombre"}
        style={styles.data_input}
        autoCorrect={false}
        blurOnSubmit
        placeholderTextColor="#777"
        autoCapitalized="words"
        multiline={true}
        onChangeText={(e) => setStateNombre(e.target.value)}
      />

It seems you are combined both Class Components and Functional components Together
Why I am Saying your Using useState from Functional components and this.setStateNombre from Class Components + Functional components.Its some kind of weird code.

when it comes to your Problem First log(JSON.stringify(param_nombre))

and For TextInput [components and uncontrolled components]

<TextInput
        value={stateNombre}
        id="text_nombre"           
        placeholder={"Nombre"}
        style={styles.data_input}
        autoCorrect={false}
        blurOnSubmit
        placeholderTextColor="#777"
        autoCapitalized="words"
        multiline={true}
        onChangeText={(e) => setStateNombre(e.target.value)}
      />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文