Giter VIP home page Giter VIP logo

Comments (4)

SMJ93 avatar SMJ93 commented on June 12, 2024

I've managed to get it working with the following:

const [currentPageNumber, setCurrentPageNumber] = useState(1);
const [previousTransactions, setPreviousTransactions] = useState([]);

// Endpoint
const [{data, loading}, postTransactions] = useAxios(
    {
      url: '/transactions',
      method: 'post',
    },
    {manual: true},
  );

// Fetch transactions on mount
  useEffect(() => {
    postTransactions();
  }, [postTransactions]);

// Fetch more transactions when scroll to end of flatlist
  const fetchMoreTransactions = () => {
    if (data?.pagination) {
      const nextPageNumber = currentPageNumber + 1;
      if (nextPageNumber <= data.pagination.last_page) {
        postTransactions({
          params: {
            page: nextPageNumber,
          },
        });
        setCurrentPageNumber(nextPageNumber);
        setPreviousTransactions(allTransactions);
      }
    }
  };

// Add new page of transactions to previous transactions
  const allTransactions = [...previousTransactions];
  if (data?.transactions) {
    allTransactions.push(...data?.transactions);
  }

// use allTransactions as data
return (
         <FlatList
            keyExtractor={(_item: any, index: number) => `${index}`}
            data={allTransactions}
            renderItem={({item}) => <TransactionGroup group={item} />}
            onEndReachedThreshold={0.5}
            onEndReached={fetchMoreTransactions} />
)

It just feels long / not very performant

from axios-hooks.

simoneb avatar simoneb commented on June 12, 2024

Why don't you use automatic requests (no manual: true) and provide the parameters to the request configuration using the values coming from the state? That way, whenever you change the state in a way that affects the request configuration the hook will request new data.

You will still need to handle adding the new data to the existing data, but this is more of an application logic concern, you're doing infinite scrolling rather than pagination.

from axios-hooks.

SMJ93 avatar SMJ93 commented on June 12, 2024

Hey @simoneb!

Because I need to trigger the request from multiple places - pull down to refresh, on load screen, on scroll to end etc.

Yeah sorry, I always combine pagination + infinite scrolling. In this case I want to do infinite scrolling :)

I'll use the above implementation for now and close the issue, was just curious to see if there was an axios hook that would handle it for me.

from axios-hooks.

simoneb avatar simoneb commented on June 12, 2024

You can combine both approaches though. Clearly if you want infinite scrolling you will have to keep some local state. I've created two examples here:

from axios-hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.