Created
October 29, 2019 16:28
-
-
Save SyNeto/ddbec28afd941d237236c67e3c110abe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { onMount } from 'svelte'; | |
import { posts } from './blogStore.js'; | |
onMount(async () => { | |
const data = await fetchPosts(); | |
posts.set(data); | |
}) | |
const fetchPosts = async (url=null) => { | |
const res = await fetch(url || 'http://localhost:8000/api/posts/'); | |
return await res.json(); | |
} | |
const handleNextPage = async (url) => { | |
const nextPage = await fetchPosts(url); | |
posts.update(x => ({...x, ...nextPage})); | |
} | |
</script> | |
<h1>Post List</h1> | |
<p>{JSON.stringify($posts.results)}</p> | |
<ul> | |
{#each $posts.results as post} | |
<li>{post.title}</li> | |
{/each} | |
</ul> | |
<p> | |
<span on:click={() => handleNextPage($posts.previous)}>prev</span> | | |
<span on:click={() => handleNextPage($posts.next)}>next</span> | |
</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment