- Sep 2020
-
svelte.dev svelte.dev
-
svelte.dev svelte.dev
-
You're not limited to using $count inside the markup, either — you can use it anywhere in the <script> as well, such as in event handlers or reactive declarations.
Don't forget to make the statement reactive. Referencing $count is not enough to get it to be re-run on update!!
Example:
$: console.log(`the count is ${$count}`);
-
-
www.barbarianmeetscoding.com www.barbarianmeetscoding.com
-
developer.mozilla.org developer.mozilla.org
-
-
async function onEdit() { editing = true // enter editing mode await tick() nameEl.focus() }
-
-
illright.github.io illright.github.io
-
www.reddit.com www.reddit.com
-
Stores are global state. While context is local state.
-
Notice it's not related to components. Another crucial difference is that it's accessible from outside of components. And good way to determine where goes where is to ask yourself, can this particular state and functionality still makes sense outside of the displayed component?
-
-
github.com github.com
-
Write svelte components in jsx.
-
-
github.com github.com
-
This package exposes an hyperscript compatible function: h(tag, properties, ...children) which returns a svelte component.
-
-
svelte.dev svelte.dev
-
In this app, we have a <Hoverable> component that tracks whether the mouse is currently over it. It needs to pass that data back to the parent component, so that we can update the slotted contents. For this, we use slot props.
Tags
Annotators
URL
-
-
svelte.dev svelte.dev
-
In fact, you might use the two together. Since context is not reactive, values that change over time should be represented as stores:
-
-
svelte.dev svelte.dev
-
The previous example contained a default slot, which renders the direct children of a component. Sometimes you will need more control over placement, such as with this <ContactCard>. In those cases, we can use named slots.
This is a nicer solution than react children props, which is only clean if you pass in a single child.
The React children prop is an unregulated wild west where people are free to use the prop almost any way they want (including passing in a function).
I kind of like how Svelte provides a standard, consistent API, which doesn't have the limitations of React childern.
-
-
svelte.dev svelte.dev
-
For interoperability with RxJS Observables, the .subscribe method is also allowed to return an object with an .unsubscribe method, rather than return the unsubscription function directly.
-
-
svelte.dev svelte.dev
-
Components with TypeScript can be type-checked with the svelte-check command
-
-
-
github.com github.com
Tags
Annotators
URL
-
-
github.com github.com
-
To type the variable, do this
-
-
codechips.me codechips.me
-
"watch:check": "svelte-check --watch"
-
-
github.com github.com
-
To add documentation on a Svelte component that will show up as a docstring in LSP-compatible editors, you can use an HTML comment with the @component tag:
-
-
www.npmjs.com www.npmjs.com
-
css-tricks.com css-tricks.com
Tags
Annotators
URL
-
-
github.com github.com
-
github.com github.com
-
def svelte_compontent(name, props) content_tag(:div, nil, id: "svelte-#{name.dasherize}-root", 'data-props': props.to_json) end
-
Tags
Annotators
URL
-
-
github.com github.com
-
github.com github.com
Tags
Annotators
URL
-
- Aug 2020
-
svelte.dev svelte.dev
Tags
Annotators
URL
-
-
svelte.dev svelte.dev
-
It's recommended to put the fetch in onMount rather than at the top level of the <script> because of server-side rendering (SSR). With the exception of onDestroy, lifecycle functions don't run during SSR, which means we can avoid fetching data that should be loaded lazily once the component has been mounted in the DOM.
-
-
svelte.dev svelte.dev
-
Now, when the user interacts with the keypad, the value of pin in the parent component is immediately updated.
bind a value to a prop
Tags
Annotators
URL
-
- Jul 2020
-
svelte.dev svelte.dev
-
A # character always indicates a block opening tag. A / character always indicates a block closing tag. A : character, as in {:else}, indicates a block continuation tag.
Tags
Annotators
URL
-
-
svelte.dev svelte.dev
-
If you have an object of properties, you can 'spread' them on to a component instead of specifying each one: <Info {...pkg}/>
-
-
svelte.dev svelte.dev
-
Take the case of the <input> element in this component — we could add an on:input event handler that sets the value of name to event.target.value, but it's a bit... boilerplatey
-
-
svelte.dev svelte.dev
-
But that's a lot of code to write, so Svelte gives us an equivalent shorthand — an on:message event directive without a value means 'forward all message events'.
-
-
svelte.dev svelte.dev
-
preventDefault — calls event.preventDefault() before running the handler. Useful for client-side form handling, for example. stopPropagation — calls event.stopPropagation(), preventing the event reaching the next element passive — improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so) capture — fires the handler during the capture phase instead of the bubbling phase (MDN docs) once — remove the handler after the first time it runs self — only trigger handler if event.target is the element itself
-
-
svelte.dev svelte.dev
-
In some frameworks you may see recommendations to avoid inline event handlers for performance reasons, particularly inside loops. That advice doesn't apply to Svelte — the compiler will always do the right thing, whichever form you choose.
-
-
svelte.dev svelte.dev
-
Any top-level statement (i.e. not inside a block or a function) can be made reactive by prefixing it with the $: JS label syntax.
-
-
github.com github.com
-
github.com github.com
Tags
Annotators
URL
-
-
svelte.dev svelte.devSvelte1
-
Shorthand attributes It's not uncommon to have an attribute where the name and value are the same, like src={src}. Svelte gives us a convenient shorthand for these cases: <img {src} alt="A man dances.">
Tags
Annotators
URL
-
-
docdrop.org docdrop.org
-
Made analogy with internal combustion engine, which has 1000s of parts, with the "radical simplicity" approach taken by Tesla: they use an electric motor, which only has 2 components!
comparison: Sapper vs. Gatsby
-
-
rethinking-reactivity.surge.sh rethinking-reactivity.surge.sh
Tags
Annotators
URL
-
-
svelte-native.technology svelte-native.technology
-
github.com github.com
-
sapper.svelte.dev sapper.svelte.dev
-
https://github.com/sveltejs/sapper
Like Gatsby.
-
- Jun 2020
-
www.reddit.com www.reddit.com
-
I was just expressing that, even thought I like React, I dread having to still manually handle everything, instead of just using a directive, a la Vue.JS. This is what I consider boilerplate. Hence my comment on how I could leave React for Svelte just because of that. Clearly a Svelte side-by-side code comparison shows a much cleaner code for Svelte.
-
<script> let a = 1; let b = 2; </script> <input type="number" bind:value={a}> <input type="number" bind:value={b}> <p>{a} + {b} = {a + b}</p>
-
I'm saying I'm ready to switch just because of the clarity of the code.
-
-
medium.com medium.com
-
State management is also easier. Instead of importing hooks and using setters, you just define a property within the script tags. You then change the value by re-assigning it (not mutating the original value).
-
But it’s impossible to argue with the value binding. You don’t have to worry about defining the value property and an onChange event for an input box in Svelte, bind:value does it all
-