Tan Li Hau 🤔 Profile picture
Jul 7, 2020 15 tweets 14 min read Read on X
Been using @reactjs for years, here is how @sveltejs code is different from @reactjs

1️⃣ Stateless Component ImageImage
2️⃣ Fragment

😍 You don't need a Fragment to group multiple elements in @sveltejs ImageImage
3️⃣ Inline styles

🙆‍♂️ You can extend and manipulate the style object in @reactjs
🙅‍♂️ It's a bane to copy the styles from the DOM into the code after tweaking it in the browser devtools
😍 So much easier when you can just copy the inline styles from browser into your code. ImageImage
4️⃣ className

🙅‍♂️ className instead of class somehow confuses new web devs that `className` is a html attribute (similarly, htmlFor 🙈)
😍 So much easier to conditionally add class names to element in @sveltejs ImageImage
5️⃣ Expressions

📝 {} for embedding expressions in JSX
📝 {} for embedding data in @sveltejs template

😜 It's the same! ImageImage
6️⃣ dangerouslySetInnerHtml

🙆‍♂️ It feels more dangerous in @reactjs to inline data as html
🙅‍♂️ Somehow you always need a element container to set innerHtml in @reactjs 😅
😍 You can insert HTML anywhere with {@\html} ImageImage
7️⃣ ref

📝 in @reactjs, you use ref to get the reference of a DOM node / component instance
👉 ref.current to get the reference

📝 in @sveltejs, you use `bind:this` to bind the reference to a variable ImageImage
8️⃣ props

📝 in @reactjs, functional component's props is passed in via arguments
📝 in @sveltejs, you use `export` to define props

🤔 in the example, Component has 2 props, `value` and `max`, and max has a default value of 5. ImageImage
9️⃣ rest and spread props

📝 in @reactjs, you use rest destructuring `...` to get the rest of the props, and spread operator `...` to spread an object into props

📝 the rest of the props is available as the magic global `$$restProps` ImageImage
1️⃣0️⃣ state

📝 @reactjs useState hook returns the state and a setter to sets the state
📝 in @sveltejs you declare a variable as the state, update the variable directly to update the state ImageImage
1️⃣1️⃣ Derived state

📝 In @reactjs, you can have a state derived from another state, and if the operation is expensive, useMemo to memoise the operation

📝 In @sveltejs, you can reactively declare a variable, whose value will be reactively updated based on its dependency ImageImage
1️⃣2️⃣ Logic - conditional rendering

📝 In @reactjs, you ternary expression or logical and (&&) short circuit to conditionally render elements
📝 In @sveltejs, you use the {#\if} logic block ImageImage
1️⃣3️⃣ Logic - list rendering

📝 In @reactjs , you use .map over an array to repeat elements
📝 In @sveltejs , you use the {#\each} logic block

✨ you can object destructure items of the array easily ImageImage
1️⃣4️⃣ Logic - list rendering

📝 In @reactjs, you add the key attribute in the mapped elements to help react identify which items have changed, added or removed
📝 in @sveltejs, you add a add a bracket (...) inside the {#\each} block ImageImage
1️⃣5️⃣ Logic - asynchronous rendering

📝 In @reactjs, to asynchronously wait for a promise, to rendering loading state, and result / error state, you'll have to manage through states, or abstract them out into a custom hook
📝 In @sveltejs, you can manage them through {#\await} ImageImage

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Tan Li Hau 🤔

Tan Li Hau 🤔 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @lihautan

Jul 6, 2021
If you have a promise waiting for your data, {#await} block makes it easy to express your content in the loading 🕑, resolved ✅ and rejected ❌ state

So, let's talk about the {#await} block in @sveltejs

👇🧵[THREAD]
Here's the general structure for the {#await} block

🕑 {#await}
✅ {:then}
❌ {:catch}
👋 {/await}

[1/4] <script>   let promise; </s...
All the states are optional, so you could have

😜 no loading state
😆 no resolved state
😅 no rejected state

[2/4] <script>   let promise; </s...<script>   let promise; </s...<script>   let promise; </s...
Read 8 tweets
Jul 5, 2021
We've seen how we can use {#each} to repeat content in @sveltejs ,

let's talk about the keyed each block, and what does it mean to have a key for each block



👇🧵[THREAD]
📹If you prefer learning through video, the following thread is a summary from my @YouTube video

💡 You can always subscribe to my channel to get notified for the next awesome content!

👉👉 youtube.com/lihautan?sub_c…
Read 14 tweets
Jul 1, 2021
HTML doesn't have a way of expressing logic, like conditionals and loops. @sveltejs does.

Let's take a look at the 1st logic block in @sveltejs, 👉 {#if} 👈

👇🧵 [THREAD]
If you prefer watching video, the following thread is a summary from my @YouTube video
💡{#if} is our 1st logic logic block in @sveltejs
💡it allow us to conditional render content
💡if comes with else, else if, just like you would expect in JavaScript <script>   let value = 42; </script>  {#if value === 42}   <
Read 8 tweets
Jun 29, 2021
How do you pass dynamic data when you forward slots in @sveltejs?
Let's recap, here is how you forward slots
and here is how you pass dynamic data into slots

Read 11 tweets
Apr 5, 2021
How much do you know about the 7 event modifiers in @sveltejs?

Let's take a look what you need to write if you don't have them 👇 Image
1️⃣ preventDefault

this is equivalent of calling event.preventDefault() on the start of the event handler Image
2️⃣ stopPropagation

this is equivalent of calling event.stopPropagation() on the start of the event handler Image
Read 9 tweets
Aug 15, 2020
Another week on #CSSpodcast, this weeks topic is CSS Functions 😍

🧵
CSS Functions provides runtime contextual expressions that return dynamic real-time value per the state of the browser per user in that moment
- global space, no need import
- can be nested, calc(var(--var))
- typed, eg: `rotate(45px)` will not work
- keep the function live, recompute on value changes and updates
- many of them are pure functions, however, counter-example: `counter()`
Read 20 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(