Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature?]: Improve ergonomics of clientOnly function with a helper. #1619

Open
2 tasks done
MengLinMaker opened this issue Sep 2, 2024 · 3 comments
Open
2 tasks done
Labels
enhancement New feature or request

Comments

@MengLinMaker
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

According to solid-docs-next/issues/840, clientOnly requires at least 2 files wit the use of import()

However, this is quite inconvenient for experimenting with CSR vs SSR.

So I'd like to propose a wrapper which is much simpler to use.

Examples 🌈

The useClient wrapper could be implemented in this way:

import { clientOnly } from '@solidjs/start'
import { Component } from 'solid-js'

// Client side render wrapper
type ResolveClientComponent<T> = (value: {
  default: Component<T>
}) => void

export const useClient = <T,>(App: Component<T>) => {
  return clientOnly(() => {
    return new Promise((resolve: ResolveClientComponent<T>) => {
      resolve({ default: App })
    })
  })
}

Usage:

const ComponentSSR =() => { ... }

const ComponentCSR = useClient(() => { ... })

Motivation 🔦

This feature allows quick switching between client side rendering or server side rendering without the overhead of adding additional files and import.

@MengLinMaker MengLinMaker added the enhancement New feature or request label Sep 2, 2024
@lxsmnsyc
Copy link
Member

lxsmnsyc commented Sep 2, 2024

I don't think this merits being a built-in primitive.

One can also do this and is a lot simpler:

import { onMount, createSignal, Show } from 'solid-js';

function clientComponent<T>(Comp: Component<T>): Component<T> {
  return function (props: T) {
    const [flag, setFlag] = createSignal(false);
    
    onMount(() => setFlag(true));
    
    return <Show when={flag()}><Comp {...props} /></Show>;
  }
}

You know what, I'll add this to solid-use/client-only

@madaxen86
Copy link

madaxen86 commented Sep 3, 2024

Plus you don't need 2x files you can just export from the same file like:

//my-component.tsx
export default function MyComponent() {
//...
return "Some JSXElement"
}


export const MyClientOnlyComponent = clientOnly( () => import("./my-component") )

@g-mero
Copy link

g-mero commented Sep 10, 2024

I don't think this merits being a built-in primitive.

One can also do this and is a lot simpler:

import { onMount, createSignal, Show } from 'solid-js';

function clientComponent<T>(Comp: Component<T>): Component<T> {
  return function (props: T) {
    const [flag, setFlag] = createSignal(false);
    
    onMount(() => setFlag(true));
    
    return <Show when={flag()}><Comp {...props} /></Show>;
  }
}

You know what, I'll add this to solid-use/client-only

This gives me a 'Hydration Mismatch' error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants