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

refactor : Remove potential issues by replacing flushSync and passing ButtonComponents as props directly #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

maxiim3
Copy link

@maxiim3 maxiim3 commented Feb 15, 2024

A little improvement :

 {BUTTONS?.map((Comp) => {
        return (
            <CardComponent
                key={Comp.name}
                ChildComponent={Comp} />
        );
    })}

I had to make the Home component "client" to avoid SSR issues.
I removed getCode function that uses flushSync.
Instead I wrapped the ChildComponent (Button) into a div and added a parentRef reference.
So I could target the ChildNode and extract the HTML code as string using outerHTML.

const parentRef = useRef<HTMLDivElement>(null);
//...
const onCopy = () => {
        const code = parentRef.current?.childNodes[0]!
        const htmlElement = ReactDOM.findDOMNode(code)! as Element
        const html = htmlElement.outerHTML
        //...
        navigator.clipboard.writeText(html);
        //...
    };
//....
 <div ref={parentRef}  >
      <ChildComponent />
  </div>

Node that I forgot to make a separate commit for the formatting caused by my IDE, so that there are a lot of changes due to space and formatting. Sorry about that...

TButton is just a type for the exported BUTTONS

export type TButton = (() => JSX.Element);
export const BUTTONS: TButton[] = [

Copy link

vercel bot commented Feb 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
buttons ❌ Failed (Inspect) Feb 15, 2024 11:19am

@ibelick
Copy link
Owner

ibelick commented Mar 6, 2024

Hi @maxiim3 ,

Thanks for your contribution! I noticed you're using ReactDOM.findDOMNode, which is deprecated too.
Using callback refs or forwarded refs to access the DOM node could be a better choice.

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

Successfully merging this pull request may close these issues.

2 participants