Comments (4)
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.
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.
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.
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:
- pagination: https://codesandbox.io/s/axios-hooks-pagination-1wk3u
- infinite scrolling: https://codesandbox.io/s/axios-hooks-infinite-scrolling-42nw6
from axios-hooks.
Related Issues (20)
- Release pending!
- Refetch "expression is not callable" HOT 2
- Why I'm getting 2 rendered using this hook, when component mounts? HOT 9
- Release pending!
- Prevent CanceledError and redundant HTTP calls for identical request data HOT 1
- Support for API polling HOT 1
- type error HOT 9
- Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) HOT 1
- How to configure SSR with NextJS? HOT 3
- "Uncaught runtime errors: Cannot read properties of undefined (reading 'data')" after upgrading to CRA 5.0.1 & Node 18 HOT 6
- Not usable within the US? HOT 1
- Error from a dynamically loaded uri HOT 3
- Release pending!
- Release pending!
- useAxios hook sending in 2 requests, the first is cancelled immediately HOT 3
- Release pending! HOT 3
- Release pending!
- Release pending!
- Type error: MouseEvent using refetch and provided example from docs HOT 2
- Still an issue with both `executeGet` and `execute` methods when executed manually HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axios-hooks.