Comparing Svelte and React. How do they compare in terms of ideology, behaviour and syntax?
A compiler approach to frontend frameworks.
MyComponent.tsx
+ MyComponent.css
-> MyComponent.svelte
..button.s-hsgb1785
instead of .button-hsgb1785
.$: {}
) matter.
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>