- Oct 2023
-
github.com github.com
-
-
- Sep 2023
-
sveltejs.github.io sveltejs.github.io
-
sveltejs.github.io sveltejs.github.io
-
sveltejs.github.io sveltejs.github.io
-
web.archive.org web.archive.org
-
```svelte
<script> $: getFirstValue = () => { // here be calculations // that does not fit on one line return result } $: getSecondValue = () => { // here be calculations // that does not fit on one line return result } // will rerun everytime getFirstValue or getSecondValue is updated because of reactivity $: value = getFirstValue() || getSecondValue() </script> <div id="app"> {value} </div>```
-
if the function declaration is reactive, then Svelte will redeclare the function everytime something within it changes, which makes the function rerun in the markup
```svelte
<script> $: giveMeSomething = () => { //... some code return something } </script> <div id="app"> {giveMeSomething()} </div>```
-
-
developer.mozilla.org developer.mozilla.org
-
sveltesociety.dev sveltesociety.dev
-
www.youtube.com www.youtube.com
-
-
mistlog.medium.com mistlog.medium.com
-
mistlog.github.io mistlog.github.io
-
svelte.dev svelte.dev
-
Runes are an additive feature, but they make a whole bunch of existing concepts obsolete: the difference between let at the top level of a component and everywhere else export let $:, with all its attendant quirks different behaviour between <script> and <script context="module"> the store API, parts of which are genuinely quite complicated the $ store prefix $$props and $$restProps lifecycle functions (things like onMount can just be $effect functions)
-
-
www.youtube.com www.youtube.comYouTube1
Tags
Annotators
URL
-
-
www.100ms.live www.100ms.live
Tags
Annotators
URL
-
-
-
```svelte
<script> let m = { x: 0, y: 0 }; function handleMousemove(event) { m.x = event.clientX; m.y = event.clientY; } </script> <div on:mousemove={handleMousemove}> The mouse position is {m.x} x {m.y} </div> <style> div { width: 100%; height: 100%; } </style>```
```js / React Hook - sample code to capture mouse coordinates/
import React, { useState, useCallback, useEffect, useRef } from 'react';
function useEventListener(eventName, handler, element = window) { // Create a ref that stores handler const savedHandler = useRef();
// Update ref.current value if handler changes. // This allows our effect below to always get latest handler ... // ... without us needing to pass it in effect deps array ... // ... and potentially cause effect to re-run every render. useEffect(() => { savedHandler.current = handler; }, [handler]);
useEffect(() => { // Make sure element supports addEventListener const isSupported = element && element.addEventListener; if (!isSupported) return;
// Create event listener that calls handler function stored in ref const eventListener = event => savedHandler.current(event); // Add event listener element.addEventListener(eventName, eventListener); // Remove event listener on cleanup return () => { element.removeEventListener(eventName, eventListener); }; }, [eventName, element] // Re-run if eventName or element changes
); }
export default function App() { // State for storing mouse coordinates const [coords, setCoords] = useState({ x: 0, y: 0 });
// Event handler utilizing useCallback ... // ... so that reference never changes. const handler = useCallback( ({ clientX, clientY }) => { // Update coordinates setCoords({ x: clientX, y: clientY }); }, [setCoords] );
// Add event listener using our hook useEventListener('mousemove', handler);
return (
The mouse position is ({coords.x}, {coords.y})
); } ```
-
-
coderpad.io coderpad.io
-
github.com github.com
-
Otherwise, you can manually call goto instead of history.replaceState is a good way to maintain client-side navigation (no reloads).
-
I highly recommend using GET forms to implement this as SvelteKit will intercept and retain client-side navigation on form submission. https://kit.svelte.dev/docs/form-actions#get-vs-post Submitting the form will update the URL with the search parameters matching the input names and values.
```html
<form action="/baz"> <label> Query <input name="query"> </label> <label> Sort <input name="sort"> </label> <label> Order <input name="order"> </label> <label> Page <input name="page"> </label> </form>```
Tags
Annotators
URL
-
-
stackoverflow.com stackoverflow.com
-
```js let query = new URLSearchParams($page.url.searchParams.toString());
query.set('word', word);
goto(
?${query.toString()}
); ```js $page.url.searchParams.set('word',word); goto(`?${$page.url.searchParams.toString()}`);
-
-
timdeschryver.dev timdeschryver.dev
-
blog.logrocket.com blog.logrocket.com
Tags
Annotators
URL
-
-
kaviisuri.com kaviisuri.com
Tags
Annotators
URL
-
- Aug 2023
-
svelte.dev svelte.dev
Tags
Annotators
URL
-
-
developer.mozilla.org developer.mozilla.org
-
investcoder.com investcoder.com
-
www.youtube.com www.youtube.comYouTube1
Tags
Annotators
URL
-
-
dev.to dev.to
-
dexie.org dexie.org
Tags
Annotators
URL
-
-
joyofcode.xyz joyofcode.xyz
-
flowbite-svelte.com flowbite-svelte.com
Tags
Annotators
URL
-
-
stackoverflow.com stackoverflow.com
-
```svelte
<script> let wrapper; </script> <div bind:this="{wrapper}"></div>```
-
-
joyofcode.xyz joyofcode.xyz
Tags
Annotators
URL
-
-
www.sveltedevtools.com www.sveltedevtools.com
Tags
Annotators
URL
-
-
-
SvelteKit will automatically await the fetchPost call before it starts rendering the page, since it’s at the top level. However, it won’t wait for the nested fetchComments call to complete – the page will render and data.streamed.comments will be a promise that will resolve as the request completes. We can show a loading state in the corresponding +page.svelte using Svelte’s await block:
<script lang="ts"> import type { PageData } from './$types'; export let data: PageData; </script> <article> {data.post} </article>js export const load: PageServerLoad = () => { return { post: fetchPost(), streamed: { comments: fetchComments() } }; };
```svelte{#await data.streamed.comments} Loading... {:then value} <br />
-
{#each value as comment}
- {comment} {/each}
{/await} ```
Tags
Annotators
URL
-
-
news.ycombinator.com news.ycombinator.com
-
-
Tags
Annotators
URL
-
-
authjs.dev authjs.devAuth.js1
Tags
Annotators
URL
-
-
community.cloudflare.com community.cloudflare.com
-
stackblitz.com stackblitz.com
Tags
Annotators
URL
-
-
twitter.com twitter.com
Tags
Annotators
URL
-
-
mvolkmann.github.io mvolkmann.github.io
Tags
Annotators
URL
-
-
storybook.js.org storybook.js.org
-
storybook.js.org storybook.js.org
Tags
Annotators
URL
-
-
storybook.js.org storybook.js.org
-
svelte.dev svelte.dev
Tags
Annotators
URL
-
-
auth0.com auth0.com
Tags
Annotators
URL
-
-
css-tricks.com css-tricks.com
-
sveltequery.vercel.app sveltequery.vercel.app
Tags
Annotators
URL
-
-
svelte.dev svelte.dev
-
stackoverflow.com stackoverflow.com
- Jul 2023
-
lihautan.com lihautan.com
Tags
Annotators
URL
-
-
vercel.com vercel.com
Tags
Annotators
URL
-
-
github.com github.com
Tags
Annotators
URL
-
-
github.com github.com
-
css-tricks.com css-tricks.com
Tags
Annotators
URL
-
-
blog.logrocket.com blog.logrocket.com
Tags
Annotators
URL
-
-
learn.svelte.dev learn.svelte.dev
Tags
Annotators
URL
-
-
blog.logrocket.com blog.logrocket.com
-
blog.logrocket.com blog.logrocket.com
-
jonasclaes.be jonasclaes.be
-
developers.cloudflare.com developers.cloudflare.com
-
kit.svelte.dev kit.svelte.dev
-
kit.svelte.dev kit.svelte.dev
Tags
Annotators
URL
-
-
github.com github.com
-
blog.logrocket.com blog.logrocket.com
Tags
Annotators
URL
-
-
geoffrich.net geoffrich.net
-
```html
<script> let counter = 0; let evens = 0; $: { if (counter > 10) { break $; } if (counter % 2 === 0) { evens++; } } </script><button on:click={() => (counter++)}> Increment </button>
Counter: {counter}, evens before 10: {evens}
```
Tags
Annotators
URL
-
-
kit.svelte.dev kit.svelte.dev
Tags
Annotators
URL
-
-
svelte.dev svelte.dev
-
-
gist.github.com gist.github.com
- Jun 2023
-
www.enterspeed.com www.enterspeed.com