I can make a api call to grab all the posts and store that in an array in vuex state. Now if I want to show a single post I can use filter method to show that post. Should I use the filter method or should I make an api call for that post. If I need to make an api call please explain to me.
Thanks.
@rifat If you want to show an individual post, then it’s best to request that post from your API so that you have the up-to-date version, rather than one pulled down into a VueX store.
It also means you don’t have to pull every post from your API to your front-end, and force the user to keep that massive amount of data in their browser’s process, which is just going to slow things down if they have many tabs open or using a low-powered device with not as much memory like a mobile phone.
Q1. Do you use the API as 'Single source of truth' ?
Yes: Request from the API. Create an endpoint to get a single resource. /api/posts/{id}
No: Look at Q2.
Q2. Can the content of the post change between the first request (all of them) and you wanting to show only one?
Yes: Request from the API.
No: Look at Q3.
Q3. Do you have all the required data to show single post ?
Yes: Pay attention to what @martinbean said.
No: Request from the API.