【问题标题】:Is there an error in my code that explains why TouchableOpacity doesn't seem to be working?我的代码中是否有错误解释了为什么 TouchableOpacity 似乎不起作用?
【发布时间】:2020-07-15 16:40:43
【问题描述】:

嗨????我目前正在关注 ShoppingList 应用程序项目/React Native 速成课程,我已经到了他的 Touchable Opacity 正在工作而我的不是:(

我很好奇我是否犯了错误,或者我已经并排检查了我的代码与视频,但可能遗漏了一些东西。非常感谢任何帮助!

import React, { useState } from 'react';
import { 
  View,
  Text, 
  StyleSheet, 
  TextInput, 
  TouchableOpacity, 
} from 'react-native';
import Icon from 'react-native-vector-icons/dist/FontAwesome';

const AddItem = ({title,addItem}) => {
    const [text, setText] = useState('');

    const onChange = textValue => setText(textValue);

  return (
    <View>
        <TextInput 
            placeholder="Add item..." 
            style={styles.input} 
            onChangeText={onChange}
        />
        <TouchableOpacity 
            style={styles.btn} 
            onPress={() =>
              addItem(text)}>
          <Text style={styles.btnText}>
              <Icon name="plus" size=
                  {20} /> Add Item
          </Text>
        </TouchableOpacity>
    </View>
  );
};



const styles = StyleSheet.create({
  input: {
      height: 60,
      padding: 8,
      fontSize: 16,
  },
  btn: {
      backgroundColor: '#c2bad8',
      padding: 9,
      margin: 5,
  },
  btnText: {
      color: 'darkslateblue',
      fontSize: 20,
      textAlign: 'center',
  },
});

export default AddItem;

【问题讨论】:

  • 你能展示一下addItem的实现吗?

标签: reactjs react-native native touchableopacity


【解决方案1】:

在您的 Textinput 中,您没有通过函数调用传递文本

这样做

<TextInput 
    placeholder="Add item..." 
    style={styles.input} 
    onChangeText={() => onChange(text)}
/>

【讨论】:

  • 你得到答案了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多