To-Do List
I have a parent with a useState called tasks (array).
And for each of those items (task) in the array, I am displaying a component with the item data and a delete button.
Now each component(child) has a delete button. But the array with the tasks is in the parent class so I can't delete the task or that component as the button is inside the component (child).
Any solutions for this problem?
Note: I have tried to find a solution but I can't seem to find it. SO just tell me what to do, the whole code is here.
The parts that need help are commented
Parent:
import React, { useState, useEffect } from "react";
import {
  Keyboard,
  KeyboardAvoidingView,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  TextInput,
  TouchableOpacity,
  View,
} from "react-native";
import Task from "./components/Task";
export default function App() {
  const [tasks, setTasks] = useState([]);
  const [newTask, setNewTask] = useState();
  const AddTask = () => {
    Keyboard.dismiss();
    if (newTask == null) return;
    setTasks([...tasks, { name: newTask }]);
    setNewTask(null);
  };
  const removeTodo = (index) => {
    let CopyLi = [...tasks];
    CopyLi.splice(index, 1);
    setTasks(CopyLi);
  };
  return (
    <View style={styles.container}>
      <View style={styles.taskWrapper}>
        <Text style={styles.sectionTitle}>Today's task</Text>
        <ScrollView style={styles.items}>
          {/* Here is the Rendering of Child Component */}
          {tasks.map((item, index) => {
            return <Task key={index} text={item.name} />;
          })}
        </ScrollView>
      </View>
      <KeyboardAvoidingView
        behavior={Platform.OS === "ios" ? "padding" : "height"}
        style={styles.inputWrapper}
      >
        <TextInput
          style={styles.input}
          placeholder={"Add a new Task"}
          value={newTask}
          onChangeText={(text) => setNewTask(text)}
        />
        <TouchableOpacity onPress={() => AddTask()}>
          <View style={styles.btnWrapper}>
            <Text style={styles.btnIcon}>+</Text>
          </View>
        </TouchableOpacity>
      </KeyboardAvoidingView>
    </View>
  );
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#e8eaed",
  },
  taskWrapper: {
    paddingTop: 80,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: "bold",
    marginHorizontal: 20,
  },
  items: {
    marginTop: 30,
    height: "78%",
  },
  inputWrapper: {
    width: "100%",
    flexDirection: "row",
    justifyContent: "space-around",
    alignItems: "center",
    height: 100,
  },
  input: {
    paddingVertical: 15,
    paddingHorizontal: 20,
    backgroundColor: "#fff",
    borderRadius: 60,
    borderColor: "#C0C0C0",
    borderWidth: 1,
    width: "70%",
  },
  btnWrapper: {
    width: 60,
    height: 60,
    backgroundColor: "#fff",
    borderRadius: 60,
    borderColor: "#C0C0C0",
    borderWidth: 1,
    alignItems: "center",
    justifyContent: "center",
  },
  btnIcon: {
    fontSize: 30,
    transform: [{ translateY: -2 }],
    color: "#A4A4A4",
  },
});
Child:
import React, { useState } from "react";
import {
  View,
  Text,
  StyleSheet,
  CheckBox,
  TouchableOpacity,
} from "react-native";
const Task = (props) => {
  const [toggle, setToggle] = useState(false);
  return (
    <View style={styles.item}>
      <View style={styles.itemLeft}>
        <CheckBox
          value={toggle}
          onValueChange={setToggle}
          style={[{ textDecorationLine: "line-through" }, styles.square]}
        />
        <Text
          style={[styles.itemText, toggle ? styles.checked : styles.unchecked]}
        >
          {props.text}
        </Text>
      </View>
      <TouchableOpacity style={styles.itemRight}></TouchableOpacity>
      {/*Here is the Button/TouchableOpacity to delete this component */}
    </View>
  );
};
const styles = StyleSheet.create({
  item: {
    backgroundColor: "#fff",
    padding: 15,
    borderRadius: 10,
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between",
    marginBottom: 20,
    width: "90%",
    alignSelf: "center",
  },
  itemLeft: {
    flexDirection: "row",
    alignItems: "center",
    flexWrap: "wrap",
  },
  square: {
    marginRight: 10,
    transform: [{ scaleX: 1.2 }, { scaleY: 1.2 }],
  },
  itemText: {
    maxWidth: "80%",
    fontSize: 16,
  },
  checked: { textDecorationLine: "line-through" },
  unchecked: { textDecorationLine: "none" },
  itemRight: {
    width: 15,
    height: 15,
    borderColor: "#f94355",
    borderWidth: 2,
    borderRadius: 10,
    backgroundColor: "#ff2848",
  },
});
export default Task;
 
     
     
    