vadimdemedes / ink

🌈 React for interactive command-line apps
https://term.ink
MIT License
26.54k stars 598 forks source link

How to render to string #459

Open dustinlacewell opened 3 years ago

dustinlacewell commented 3 years ago

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

scalvert commented 3 years ago

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

zhanwang626 commented 3 years ago

+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.

scalvert commented 3 years ago

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

vadimdemedes commented 1 year ago

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 commented 1 year ago

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.