1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

react naitive <ScrollView>スクロールしても元の位置に戻ってしまう問題

Last updated at Posted at 2024-05-24

スクロールしても元の位置に戻ってしまう問題が解決できました
とっても簡単誰にでも解決できます!!!!

import React, { useEffect, useState, useRef } from "react";
import { View, Text, ScrollView, StyleSheet } from "react-native";
import ExpenseItem from "./ExpenseItem"; 
import { collection, onSnapshot } from "firebase/firestore";
import { db } from "../firebase/Firebase"; 

export const BuyItemsList = ({
  deleteExpense,
  selectedMonth,
  thisMonth,
}) => {
  const [expenseItems, setExpenseItems] = useState([]);
  const [loading, setLoading] = useState(true);
  const scrollViewRef = useRef(null);

  useEffect(() => {
    const fetchExpenses = () => {
      const unsubscribe = onSnapshot(
        collection(db, "expenseItems"),
        (snapshot) => {
          const expenses = [];
          snapshot.forEach((doc) => {
            expenses.push({ ...doc.data(), docId: doc.id });
          });
   
          expenses.sort((a, b) => b.time.seconds - a.time.seconds);

          setExpenseItems(expenses);
          setLoading(false);
        }
      );

      return unsubscribe;
    };

 
    fetchExpenses();
  }, []);

  if (loading) {
    return <Text>Loading...</Text>;
  }

  return (
    <View style={styles.container}>
      <Text style={{ fontSize: 20, fontWeight: "bold", marginBottom: 10 }}>
        支出一覧
      </Text>
      <ScrollView
        style={styles.scrollView}
        ref={scrollViewRef}
        contentContainerStyle={styles.contentContainer}
      >
        {expenseItems.length === 0 ? (
          <Text style={styles.noExpensesText}>何も買ってないよ</Text>
        ) : (
          expenseItems.map((expenseItem) => (
            <ExpenseItem
              key={expenseItem.docId}
              deleteExpense={deleteExpense}
              expenseText={expenseItem.text}
              expenseAmount={expenseItem.amount}
              expenseTime={expenseItem.time}
              expenseItem={expenseItem}
              selectedMonth={selectedMonth}
              thisMonth={thisMonth}
            />
          ))
        )}
      </ScrollView>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    height: 300, //これを追加//////ここだよーーー
  },
  contentContainer: {
    paddingBottom: 80, 
  },
  noExpensesText: {
    fontSize: 16,
    color: "#888",
    textAlign: "center",
    marginTop: 20,
    fontStyle: "italic",
  },
});

要らない情報が多いので今度消しておきますね
スクロールの様子についても動画を載せます

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?