mrdotb / live_react

✨ React inside Phoenix LiveView with seamless end-to-end reactivity
https://live-react-examples.fly.dev/simple
MIT License
90 stars 8 forks source link

Fix vite config #15

Closed 27medkamal closed 3 weeks ago

27medkamal commented 1 month ago

This wasn't obvious to me at first as the new vite installation guide works on the examples provided here.

However, there is an issue where if you use React.forwardRef in your components, you get the error: Uncaught TypeError: React.forwardRef is not a function

It seems that live_vue has the same config as well https://github.com/Valian/live_vue/blob/main/assets/copy/vite.config.js#L22

As for why it works otherwise without having this line, I'm not entirely sure, but going by live_vue which this vite implementation models, it seems to fix the issue.

Contributor checklist

27medkamal commented 1 month ago

Actually, this seems to break SSR. Will have to investigate more

mrdotb commented 1 month ago

Hello, I was going to say that I try this but it break SSR. Could you add an example with forward ref ? I will take a look

mrdotb commented 1 month ago

I try a simple forwardRef, useRef but I don't see any issue :thinking:

import React, { forwardRef, useRef } from 'react';

// Child component using forwardRef
const Input = forwardRef((props, ref) => {
  return <input ref={ref} {...props} />;
});

export function Simple() {
  const inputRef = useRef(null);

  const focusInput = () => {
    // This will focus the input in the child component
    inputRef.current.focus();
  };

  return (
    <div>
      <Input ref={inputRef} placeholder="Type something here..." />
      <button onClick={focusInput}>Focus Input</button>
    </div>
  );
}
27medkamal commented 1 month ago

I also tried adding it to your examples project, but it worked fine.

I'll need to debug this a bit more

27medkamal commented 1 month ago

This ended up working just fine on my end after cleaning up deps and node_modules. Not sure what happened there, so closing this for now. Sorry for the false alarm.

timadevelop commented 1 month ago

For anyone having the same issue: try to rename react folder to something like react-components & update imports and vite/ts configs to use the new folder.

@mrdotb I had the same issue in one of the projects and I believe it's because of the "react" folder in this particular case. I did not investigate further & I'm not sure what is the exact reason, maybe Vite has some issues with aliases, but in my case renaming the folder worked.

btw, it might be worth changing the react folder to something else, not only because of this problem, but also because of semantics.

mrdotb commented 1 month ago

Thanks for pinpointing this issue I will do a PR next week end.

mrdotb commented 3 weeks ago

19 fixed