DEV Community

Discussion on: 3 reasons you should try Svelte

Collapse
 
pengeszikra profile image
Peter Vivo • Edited

react with hook is 9 line, but this component can be instanced any amount, and after remove closeInterval properly. Maybe seems complex, but you can use modern vanilla JS / JSX.

import React, {useState, useEffect} from 'react';
export const Timer = ({...props}) => {
  const [seconds, setSeconds] = useState(0);
  useEffect(_=>{
    const interval = setInterval( _ => setSeconds( s => s + 1 ), 1000);
    return _ => clearInterval(interval); // refacted: Franciszek Krasnowski 
  }, []);
  return <div {...props}>{seconds}</div>;
}

implement example

import React from 'react';
import {Timer} from './timer.js';
import './style.css';

export default () => (
  <section>
    <Timer />
    <Timer style={{color: 'red'}} />
    <Timer className="custom-timer" />
  </section>
);

each timer have own state

Collapse
 
fkrasnowski profile image
Franciszek Krasnowski

React is still terse. But what iscloseInterval? Did you mean clearInterval? And I prefer using custom hooks to encapsulate โ€reference fragileโ€ behavior. For example by making useInterval hook or utilize the one from rooks package

Collapse
 
pengeszikra profile image
Peter Vivo

you right, clearInterval of course!

Collapse
 
mikenikles profile image
Mike

I used React for the first few years right after it was announced and absolutely loved it. For the past year and a half, I've used Svelte exclusively and wouldn't go back. Here's a lot more detail on why: mikenikles.com/blog/why-i-moved-fr...

As for your example with hooks: In my opinion, this is near impossible to understand without a deep knowledge of React which likely takes many hours of reading and using to acquire.

The Svelte example on the other hand is self-explanatory. With an hour of effort to work through the interactive Svelte tutorial, most people are ready to be productive.

If team efficiency, developer experience, time to market, maintainability and end user performance experience are important measures, I'd highly recommend Svelte over React in most cases.

It's important to note, and the author mentioned that too, that Svelte is less established. Fewer jobs, fewer opportunities to use it on a day job etc. However, that's the case for any new framework. Svelte is adopted quickly among big companies such as Apple and Spotify.