question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compile away hooks

See original GitHub issue

Purely an idea, but something been thinking about a lot. One of the biggest missing DX features of Mitosis is custom hooks

Interestingly, some ppl have been exploring making hooks libraries across frameworks

If you think of someone building an app in Mitosis vs a framework like Qwik directly, you miss out on one of the main composability benefits, custom hooks. Such as a react-query like hook (called Resource in Qwik and Solids) etc. I can’t see people loving building with Qwik without this key feature nearly all modern frameworks have (react, vue, svelte, qwik, solid) that Mitosis doesn’t

I realized I think we can implement this for all hooks-oriented frameworks. We may even be able to implement it for frameworks without first class hooks support.

We could do it, for instance, with another convention just like we have .lite.tsx for components, and .context.tsx for context, we could make a .hook.tsx

// ./use-fetch.hook.tsx
import { useState, onMount } from '@builder.io/mitosis'

export default function useFetch(url: string) {
  const [value, setValue] = useState(null)
  const [error, setError] = useState(null)
  const [loading, setLoading] = useState(false)

  onMount(async () => {
     setLoading(true);
     try {
       const data = await fetch(url).then(res => res.json())
     }  catch (error) {
        setError(error)
     } finally { 
       setLoading(false)
     }
     setValue(data)
  })

  return { value, loading, error };
}

which would compile to other frameworks just like you see in mitosis components name

I think translating hooks like this is pretty straightforward to everywhere that supports hooks. the big question is what about frameworks, like Angular, that don’t. I have some crazy ideas for that but need to think on a little more

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:8
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
leopiccioniacommented, Jul 20, 2022

Composition API is also Vue3 only I believe, so that’s another thing to keep in mind.

FYI Vue 2.x supports the Composition API natively since 2.7 (released weeks ago). For earlier releases, you need the @vue/composition-api plugin.

Vue 2.7 Composition API works like Vue 3 Composition API, except when otherwise noted.

1reaction
digoburigocommented, Aug 26, 2022

Imagine VueUse working in all frameworks. That would be really nice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compiling Hooks
All Hooks are compiled to a single webassembly module before they can be set onto an XRPL account. A Hook always implements and...
Read more >
Introducing Hooks - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. import React,...
Read more >
How to Build Your Own React Hooks: A Step-by-Step Guide
Want to learn how to create custom React hooks as you build fun, real-world applications? Check out The React Bootcamp.
Read more >
3. Build Hooks - Gradle Beyond the Basics [Book] - O'Reilly
Chapter 3. Build Hooks By itself, Gradle is a deeply customizable toolkit for creating custom build software. It comes with out-of-the-box conventions that ......
Read more >
Transpilers - The newline Guide to Creating a React Hooks ...
... using it for other purposes such as automating code migrations, so-called codemods, or compiling away dynamic code to static code to save...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found