【问题标题】:Editable Text in React NativeReact Native 中的可编辑文本
【发布时间】:2020-05-05 22:59:12
【问题描述】:

我是 react native 的新手,我需要一些帮助。我正在尝试处理一个 TextInput ,用户可以在其中更新他们想要的任何文本值。例如,如果用户创建了一个名为“Bar”的文件夹,但后来用户决定更改“Bar1”的名称。怎么办???

到目前为止,这是我的代码:其中 value={prosp.children} 是文本值“Bar”。 onChangeText={(text) => handleText(text)},这里的文本暂时未定义,但稍后它将是我的文本更新“Bar1”或用户写的任何内容。

import React, { useState } from "react";
import { View, TextInput} from "react-native";
import styles from "../globalStyles/Styles";

const FolderList = (props) => {
  const handleText = (value, text) => {};

  return (
    <View style={styles.homeRowFolder}>
      <View style={styles.iconAndText}>
        <TextInput
          value={props.children}
          onChangeText={(text) => handleText(text)}
          style={styles.textdisplay}
        ></TextInput>
      </View>
    </View>
  );
};

export default FolderList;

我也想使用反应钩子。 非常感谢您提前提供的帮助。欣赏。

【问题讨论】:

    标签: javascript reactjs react-native react-hooks


    【解决方案1】:

    在事件处理函数中,您需要两个参数。 valuetext

      const handleText = (value, text) => {};
    

    但只有一个从事件处理程序传递。

    onChangeText={(text) => handleText(text)}
    

    您需要更改 handleText 以便它只接收传入的文本,这应该可以工作。

    const handleText = (text) => { 
        console.log(text); // should print the updated text
    };
    

    您无需使用挂钩来处理此交互。但是,如果您正在对数据执行任何操作,例如将其保存为状态,则可以使用适当的挂钩。

    【讨论】:

    • 嗨@nipuna777 我想为新用户textInput 更新我的当前值={props.children}。另外,替换数据并将其保存到状态中。我该怎么做??
    • 你不应该更新道具。这个想法是保持数据从父组件流向子组件。您可以使用 useContext 钩子之类的东西将更改发送到父组件。 alligator.io/react/usereducer
    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多