Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

porting over some react code to my project, having hydration issues #2584

Closed
Upbolt opened this issue May 10, 2024 · 0 comments
Closed

porting over some react code to my project, having hydration issues #2584

Upbolt opened this issue May 10, 2024 · 0 comments

Comments

@Upbolt
Copy link
Contributor

Upbolt commented May 10, 2024

This is just a question, as there's probably a solution to this, but I'm not sure what's really going on here

I've got this React code:

// test.tsx
"use client";

import { useEffect, useState } from "react";

export function Foo() {
    const [foo, setFoo] = useState<HTMLDivElement | null>(null);
    const isFormControl = foo ? Boolean(foo.closest("form")) : true;

    console.log(isFormControl);
    useEffect(() => {
        console.log(isFormControl);
    }, [isFormControl]);

    return (
        <>
            <div ref={(node) => setFoo(node)}>hello world</div>
            {isFormControl && <div>hello world 2</div>}
        </>
    );
}

// HomePage.tsx
import { Foo } from "./test";

export default function HomePage() {
  return (
    <>
      {/* <form> */}
      <Foo />
      {/* </form> */}
      {/* <form> */}
      <Foo />
      {/* </form> */}
    <>
  );
}

Which I've ported like so:

#[component]
fn Foo() -> impl IntoView {
  let foo_ref = NodeRef::<html::Div>::new();

  let is_form_control = move || {
    if let Some(foo) = foo_ref.get()  {
      foo.closest("form").ok().flatten().is_some()
    } else {
      true
    }
  };

  logging::log!("{}", is_form_control());

  Effect::new(move |_| {
    logging::log!("{}", is_form_control());
  });

  view! {
    <div node_ref=foo_ref>"hello world"</div>
    <Show when=is_form_control>
      <div>"hello world 2"</div>
    </Show>
  }
}

#[component]
fn HomePage() -> impl IntoView {
  view! {
      // <form>
        <Foo />
      // </form>
      // <form>
        <Foo />
      // </form>
  }
}

The React code (using Next.js) works fine, but it doesn't seem like the case with the Leptos port because there are hydration issues 🤔

I've also tested this with Solid.js (with SolidStart) just to see if I'm thinking of the reactive system in the wrong way, but Solid also doesn't have any hydration issues

// test.tsx
import { Show, createEffect, createSignal } from "solid-js";

export default function Foo() {
    const [foo, setFoo] = createSignal<HTMLDivElement | null>(null);
    const isFormControl = () => (foo() ? Boolean(foo()!.closest("form")) : true);

    console.log(isFormControl());
    createEffect(() => {
        console.log(isFormControl());
    });

    return (
        <>
            <div ref={node => setFoo(node)}>hello world</div>
            <Show when={isFormControl()}>
                <div class="test">hello world 2?</div>
            </Show>
        </>
    );
}

// HomePage.tsx
import { Foo } from "./test";

export default function HomePage() {
    return (
        <>
          {/* <form> */}
          <Foo />
          {/* </form> */}
          {/* <form> */}
          <Foo />
          {/* </form> */}
        </>
    );
}

(albeit my Solid.js code doesn't seem to work properly when a form is detected, isFormControl always evaluates to false; at least there aren't any hydration issues 🤣)

So I'm wondering if I'm actually doing anything wrong here or if something more is going on?

@leptos-rs leptos-rs locked and limited conversation to collaborators May 10, 2024
@gbj gbj converted this issue into discussion #2585 May 10, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant