jeudi 2 mars 2023

Problems with change states (true n false) in React-Native Firebase

Im trying throgh a checkbox, change states in my Firebase bd, but it still doesnt work.

Here's my code:

export default Task = (props) => {
  const [title] = React.useState(props.title)
  const sectionId = React.useState(props.sectionId)
  const taskId = React.useState(props.taskId)
  const [complete, setComplete] = React.useState(props.complete);

  const updateCheck = async () => {
    database
      .collection('sections')
      .doc(sectionId + '')
      .collection('tasks')
      .doc(taskId + '')
      .set({
        complete: true
      })
  };

  React.useEffect(() => {
    updateCheck();
  }, [complete]);

  return (
    <TouchableOpacity
      style={styles.tasks}
      activeOpacity={0.7}
      onPress={() => setComplete(!complete)}>
      <Checkbox
        disabled={false}
        value={complete}
        onValueChange={setComplete}
        color={'#e80e3a'}
        style=
      />
      <Text style={styles.text}>{title}</Text>
    </TouchableOpacity>
  );
}

And dont worry, this props its corretly getting data from another doc.

const renderTasks = ({ item, navigation }) => <Task title={item.title} sectionId={sectionId} complete={item.complete} taskId={item.key}/>;

  const fetchTasks = async () => {
    database
      .collection('sections/'+ sectionId +'/tasks')
      .onSnapshot((querySnapshot) => {
        const list = [];

        querySnapshot.forEach((documentSnapshot) => {
          list.push({
            ...documentSnapshot.data(),
            key: documentSnapshot.id,
          });
        });
        setIsLoading(false);
        setTasks(list);
      })
  };

  React.useEffect(() => {
    fetchTasks();
  }, []);

(PS: I put it to show in console log, when the state changes, and it worked, it shows the task id and its states, but doesnt update in Firebase).

Ive tried to change set by update, but the same error continues.

Before that I also tried to use the data pull, like this:

const updateCheck = async () => {
    database
      .collection('sections/' + sectionId + '/tasks/' + taskId)
      .set({
        complete: true
      })
  };

And it worked in another docs, but this one specifically, didnt work.

If someone solve this or show me other way to create this system, I would appreciate it




Aucun commentaire:

Enregistrer un commentaire