【问题标题】:Changing the Value of text on dynamic TextInput更改动态 TextInput 上的文本值
【发布时间】:2021-01-17 04:26:30
【问题描述】:

我正在尝试使用 javscript 和 expo 添加简单的 textInput 一切都运行良好,除非尝试更改 text 的值给我一个错误

我确实尝试了很多东西,但没有任何效果

const AccountsAddScreen = props => {

  const [inputList, setInputList] = useState([{accountName : ""}]);



const handleInputChange = (e, index) => {
  const list = [...inputList]
 const {accountName, value } = e.target
list[index][accountName] = value;
setInputList(list);
};


const handleRemoveClick = index => {
  const list = [...inputList];
  list.splice(index, 1);
  setInputList(list);
}

const handleAddClick = () => {
  setInputList([...inputList, {accountName : ''}]);
}


console.log(single)

  return (
    <View style = {styles.container}>
      <Text>
       We are Testing 
      </Text>
      <View>
        {
          inputList.map((x, i) => {
            return (
              <View key = {i}>
              <TextInput 
              placeholder = 'Add Account Name'
              style = {styles.input}
              value = {x.accountName}
              onChangeText = {e => handleInputChange(e, i)}
              />
            {
              inputList.length !== 1 && <Button title = 'Remove' onPress = {()=> { handleRemoveClick(i)}}/>
            }
            {
              inputList.length - 1 === i && <Button title = 'Add' onPress = {handleAddClick} />
            }
              </View>
            );
          })
        }
      </View>
    </View>
  );
}


const styles = StyleSheet.create({
  input : {
    borderColor : 'black',
    borderWidth : 1
  }
});

export default AccountsAddScreen;

我得到的错误是

TypeError: undefined is not an object (evalating '_e$target.accountName')

【问题讨论】:

    标签: javascript react-native expo react-native-android textinput


    【解决方案1】:
    const {accountName, value } = e.target
    list[index][accountName] = value;
    

    应该是

    const { value } = e.target
    list[index].accountName = value;
    

    【讨论】:

      【解决方案2】:

      你必须像下面那样做。

        const handleInputChange = (e, index) => {
          const list = [...inputList];
      
          list[index]['accountName'] = e;
          setInputList(list);
        };
      

      'e' 保存更新文本的值,您可以简单地将其设置为您传入 i 的索引中的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-02
        • 1970-01-01
        • 2012-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多