All Projects → joshnuss → React Hooks In Svelte

joshnuss / React Hooks In Svelte

React hook examples ported to Svelte

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Hooks In Svelte

Git Hooks Js
A tool to manage and run project git hooks
Stars: ✭ 158 (-10.23%)
Mutual labels:  hooks
Svelte Table
A svelte-3 table implementation that allows sorting and filtering
Stars: ✭ 162 (-7.95%)
Mutual labels:  svelte
Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+1023.3%)
Mutual labels:  svelte
Svelte Ionic App
Ionic UI showcase app - try Ionic UI and directly go to API or source code
Stars: ✭ 159 (-9.66%)
Mutual labels:  svelte
Spotify Clone Client
A ReactJS clone application of the popular Spotify music streaming service. This application utilizes the Spotify API and the Spotify Web Playback SDK
Stars: ✭ 162 (-7.95%)
Mutual labels:  hooks
React Rough
🐇 React Components for Rough.js
Stars: ✭ 164 (-6.82%)
Mutual labels:  hooks
Svelte Chota
Svelte UI components based on super lightweight chota CSS framework.
Stars: ✭ 157 (-10.8%)
Mutual labels:  svelte
Deploy
Ansible role to deploy scripting applications like PHP, Python, Ruby, etc. in a capistrano style
Stars: ✭ 2,141 (+1116.48%)
Mutual labels:  hooks
Sveltejs Forms
Declarative forms for Svelte
Stars: ✭ 163 (-7.39%)
Mutual labels:  svelte
React Use Wizard
🧙 A React wizard (stepper) builder without the hassle, powered by hooks.
Stars: ✭ 162 (-7.95%)
Mutual labels:  hooks
Pre Git
Automatically install pre-commit / pre-push hooks in your git repo
Stars: ✭ 159 (-9.66%)
Mutual labels:  hooks
15 Puzzle
The 15-puzzle is a sliding puzzle that consists of a frame of numbered square tiles in random order with one tile missing, built in react
Stars: ✭ 161 (-8.52%)
Mutual labels:  hooks
Svelte Swipe
Svelte Swipe with zero dependencies 🔥 💥
Stars: ✭ 164 (-6.82%)
Mutual labels:  svelte
Ajari Koding
📚 Kumpulan berbagai sumber daya untuk belajar koding dari hasil karya para kreator lokal yang terpercaya dan telah dikurasi oleh komunitas PHPID
Stars: ✭ 156 (-11.36%)
Mutual labels:  svelte
React Hooks Mobx State Tree
React Hooks + MobX State Tree + TypeScript = 💛
Stars: ✭ 169 (-3.98%)
Mutual labels:  hooks
Vim Svelte
Vim syntax highlighting and indentation for Svelte 3 components.
Stars: ✭ 158 (-10.23%)
Mutual labels:  svelte
Pinst
🍺 dev only postinstall hooks (package.json)
Stars: ✭ 162 (-7.95%)
Mutual labels:  hooks
React Nprogress
⌛️ A React primitive for building slim progress bars.
Stars: ✭ 173 (-1.7%)
Mutual labels:  hooks
Zent
A collection of essential UI components written with React.
Stars: ✭ 2,133 (+1111.93%)
Mutual labels:  hooks
Simpler State
The simplest app state management for React
Stars: ✭ 68 (-61.36%)
Mutual labels:  hooks

React Hooks in Svelte

React Hook examples ported to Svelte.

useState

In Svelte, const [varName, set] = useState(initialValue) becomes let varName = initialValue. The setter function is replaced with JavaScript's assignment operator =.

React example
Svelte example

Diff of useState

useEffect

In React, there are 3 ways to useEffect().

  1. With null dependencies: useEffect(fn). This runs on every render.
  2. With an empty array as dependencies: useEffect(fn, []). This runs during mount, and cleanup function runs on unmount.
  3. With a list of dependency vars: useEffect(fn, [a, b, c]). This reavaulates whenever a dependency changes. The cleanup runs whenever dependencies change and during unmount.

This is an example of #2, where the callback runs when component is mounted and cleanup runs when unmounted.

React example
Svelte example

Diff of useEffect

useMemo

React example
Svelte example

In Svelte, all reactive statements are memoized. Instead of const var = useMemo(() => expression, dependencies), you can use $: var = expression. Notice that with Svelte, you don't need to declare the dependencies. The compiler infers them for you.

Diff of useMemo

useRef

React example
Svelte example

In Svelte, useRef() is bind:this.

Diff of useRef

useReducer

React example
Svelte example

In Svelte, useReducer() can be replaced with a writable() store. Instead of dispatching using a switch statement, functions can be defined on the store directly.

Diff of useReducer

useCallback

In React, useCallback is used to memoize functions. This is needed because event handlers are re-defined on every render.

Take this example:

// This function (component) is executed on every render
function Component() {
  // this event handler is redefined on every render
  const handleClick = () => alert("hello")
  
  // because `handleClick` is redefined on every render, `ChildComponent` will be re-rendered too. Because its `onClick` prop is considered changed.
  return <ChildComponent onClick={handleClick}/>
}

So we need to wrap handleClick in a useCallback, to give a hint to the rendering system that the handler wasn't changed.

In Svelte this isn't needed, because event handlers are declared inside <script> tags. They aren't defined in the render path and therefore arent't redefined on every render. They are defined once per component, so they work similar to how event handlers worked with React.Component.

useContext

Context in both frameworks are very similar. One difference is that context in Svelte is not reactive by default. To make it reactive, context data should be wrapped in a store.

Another difference is that context in Svelte does not insert anything into the visual component tree. There is no <Context.Provider> element like in React, instead use the setContext() function.

React example
Svelte example

Root component Diff of useEffect App

Intermediate component Diff of useEffect Toolbar

Grand-child component Diff of useEffect ThemedButton

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].