javascript - 删除Hook,自动删除数据库中的所有条目

标签 javascript reactjs react-native

我创建了一个删除 Hook 来单独删除屏幕上的一些数据,但每次加载该 Hook 时,它都会删除页面上的所有条目。我试图能够单击一个图标并删除一个项目。我通过 props 传递项目的 id,然后调用我的删除函数,但钩子(Hook)不会等待用户输入,而是删除数据库中的所有内容。如有任何帮助,我们将不胜感激。

Hook

import { useContext } from 'react';
import { Context as CertificationContext } from '../context/CertificationContext';
import { Context as CertificateInfoContext } from '../context/CertificateInfoContext';

export default () => {
  const { deleteCertification } = useContext(CertificationContext);
  const {
    state: { certificationId }
  } = useContext(CertificateInfoContext);
  console.log('Delete' + certificationId);
  const removeCertification = async id => {
    await deleteCertification(id);
  };

  return removeCertification;
};

组件

import { View, Text, StyleSheet } from 'react-native';
import { Button } from 'react-native-elements';
import Icon from 'react-native-vector-icons/Feather';
import { navigate } from '../navigationRef';
import useDeleteCertification from '../hooks/useDeleteCertification';

const Certifications = ({ title, month, description, id }) => {
  const removeCertification = useDeleteCertification();
  return (
    <View style={styles.mainContainer}>
      <View style={styles.firstContainer}>
        <Text style={styles.monthText}>{month}</Text>
        <Text style={styles.dayText}>FRI</Text>
      </View>
      <View style={styles.secondContainer}>
        <Text style={styles.title}>{title}</Text>
        <Text style={styles.subtitle}>{description}</Text>
        <View style={styles.buttonView}>
          <Button
            icon={<Icon name='trash-2' color='grey' size={15} />}
            type='clear'
            onPress={removeCertification(id)}
          />
          <Button
            icon={<Icon name='share' color='grey' size={15} />}
            type='clear'
            onPress={() => {
              console.log('Share Button');
            }}
          />
          <Button
            icon={<Icon name='edit' color='grey' size={15} />}
            type='clear'
            onPress={() => {
              navigate('EditCertification', { id });
            }}
          />
        </View>
      </View>
    </View>
  );
};

最佳答案

onPress 属性中使用箭头函数,如下所示:

<Button
  icon={<Icon name='trash-2' color='grey' size={15} />}
  type='clear'
  onPress={() => removeCertification(id)}
/>

现在您传递的是函数而不是函数的结果,因此它不会在渲染时触发

关于javascript - 删除Hook,自动删除数据库中的所有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60739968/

相关文章:

javascript - Android Studio React Native 构建失败

javascript - React native - 不能返回简单的函数?意外的标记?

javascript - React.js 从函数返回样式属性并在 render() 函数中使用它

javascript - Powerbuilder/Javascript HTML Datawindow SetItem 日期时间失败

javascript - 如何在 datatables.js 中添加子列/划分列

javascript - RxJS:将第一个源排队,直到第二个源满足谓词

javascript - 将不同的数组映射到同一个组件

javascript - rails : jQuery works in localhost but not in production on heroku

javascript - 如何在没有后端的情况下保存用户输入

javascript - 循环内的逻辑在 ReactJs 上不起作用