ink icon indicating copy to clipboard operation
ink copied to clipboard

How to render to string

Open dustinlacewell opened this issue 3 years ago • 5 comments

Is there a way to render to string? I am wanting to render to a file for documentation, etc.

dustinlacewell avatar Jul 21 '21 05:07 dustinlacewell

Would it be possible to take the same approach that ink-testing-library uses here?

scalvert avatar Aug 23 '21 22:08 scalvert

+1, I would like to let render function output a string as well, someing like:

const { stdout } = render();

So I can write the stdout content to a file.

zhanwang626 avatar Aug 24 '21 05:08 zhanwang626

It looks like @zhanwang626 wrote something! https://github.com/zhanwang626/ink-render-string

scalvert avatar Aug 25 '21 21:08 scalvert

I've looked into this and I couldn't figure out how to implement a renderToString method similar to the one in React DOM. I would expect renderToString to capture the first output without running any side effects (e.g. useEffect) and return that synchronously.

ink-testing-library however renders the Ink app as usual and just captures all the frames it renders, which wouldn't be what users of renderToString would expect in a scenario like this:

const output = renderToString(<MyApp/>);

If anyone has any ideas how to make that happen, I'd appreciate it!

vadimdemedes avatar Apr 01 '23 11:04 vadimdemedes

Going to mark this as "help wanted", in case there are some React reconciler/internals wizards that know how to disable all side effects (e.g. useEffect) when rendering a tree.

vadimdemedes avatar Apr 24 '23 07:04 vadimdemedes