svelte-vs-react-presentation Svelte Themes

Svelte Vs React Presentation

Comparing Svelte and React. How do they compare in terms of ideology, behaviour and syntax?

Svelte VS React

Comparing Svelte and React. How do they compare in terms of ideology, behaviour and syntax?

Svelte:

A compiler approach to frontend frameworks.

Good:

  • Single file components (SFC).
    • MyComponent.tsx + MyComponent.css -> MyComponent.svelte.
  • Little boilerplate.
    • Less code = less bugs.
    • Less explicit code, sometimes harder to follow.
  • No (required) runtime.
  • Built-in CSS support.
  • Scoped CSS.
  • Great documentation.

Bad(-ish):

Different:

Code examples/differences from React:

React:

import React from 'react';

export function MyComponent(props) {
  return (
    <>
      <h1>Component markup</h1>
      <p>I am the component markup</p>
    </>
  );
}

Svelte:

<script lang="ts">
</script>

<h1>Component markup</h1>
<p>I am the component markup</p>

<style>
</style>

React:

const quiz = useSelector(getQuiz);
const dispatch = useDispatch();
...
dispatch(startQuiz());

Svelte:

const store = writable(defaultState); // https://svelte.dev/tutorial/writable-stores
$: quiz = $store.quiz; // https://svelte.dev/tutorial/reactive-declarations
...
$store.quizIsStarted = true;

React:

useEffect(() => { 
  doSomething(value);
}, [value]);

Svelte:

$: { 
  doSomething(value);
} // Svelte magically figures out the dependencies.

React:

const myRef = useRef();
...
<div ref={myRef}></div>

Svelte:

let myRef;
...
<div bind:this="{myRef}"></div>

Top categories

Loading Svelte Themes