Skip to content

[Next.js 14 / React 18] How to migrate from renderToStaticMarkup method on the Client side? #58533

Answered by yakovenkomax
BorysShulyak asked this question in Help
Discussion options

You must be logged in to vote
// clientRenderToString.tsx

export const clientRenderToString = (element: React.ReactElement): Promise<string> =>
    new Promise(resolve => {
        const container = document.createElement('div');
        const renderCallback = () => {
            resolve(container.firstElementChild?.innerHTML || '');
        };
        
        ReactDOM.createRoot(container).render(<div ref={renderCallback}>{element}</div>);
    });
// useClientRenderToString.ts

type UseClientRenderToString = (
    input: React.ReactElement | React.ReactElement[],
    deps?: any[]
) => string[];

export const useClientRenderToString: UseClientRenderToString = (input, deps = []) => {
    const [htmlStringList, setHtm…

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
2 replies
@abraao31
Comment options

@BorysShulyak
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@cmdcolin
Comment options

@cmdcolin
Comment options

@yakovenkomax
Comment options

@yakovenkomax
Comment options

Answer selected by BorysShulyak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
7 participants