bfanger / svelte-preprocess-react

Seamlessly use React components inside a Svelte app
MIT License
126 stars 6 forks source link

Using hooks in Svelte #14

Closed bfanger closed 1 year ago

bfanger commented 1 year ago

Re-using React hooks inside a Svelte component.

<script lang="ts">
  import { useState } from "react";
  import { hooks } from "svelte-preprocess-react";

  const values = hooks(() => useState(0));
  $: [count, setCount] = $values;
</script>

<div>Count: {count}</div>
<button on:click={() => setCount(count + 1)}>+</button>
bfanger commented 1 year ago

React renders are asynchronous, so the initial value of the store is undefined 😕 But the hooks implementation came very close to the proposed design.