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

Function as children display #63

Closed
pascalduez opened this issue Nov 21, 2016 · 7 comments
Closed

Function as children display #63

pascalduez opened this issue Nov 21, 2016 · 7 comments
Labels

Comments

@pascalduez
Copy link
Contributor

Hi,

is react-element-to-jsx-string able to output/format a "Function as children"? (More infos)

const Foo = (props) => (
  <div>
    {props.children()}
  </div>
);

const Bar = () => (
  <Foo>{() => (
    <span>Yeah</span>
  )}</Foo>
);

Output from react-element-to-jsx-string:

<Foo />

Expected output:

<Foo>{() => (
  <span>Yeah</span>
)}</Foo>
@vvo
Copy link
Contributor

vvo commented Nov 21, 2016

I think right now this module is sort of a shallow renderer (only root level). This issue is similar to algolia/expect-jsx#24.

Ultimately we COULD be deep but it would have to be implemented.

Let me know if this makes sense.

@vvo vvo added the question label Nov 21, 2016
@pascalduez
Copy link
Contributor Author

That totally makes sense.

We are currently building our internal styleguide code samples with react-element-to-jsx-string, which allows to use one snippet for preview and code display. Some of our components use the Function as children pattern.

I'll try to evaluate how much work it is to implement this.

@vvo
Copy link
Contributor

vvo commented Jun 7, 2017

Closing this since for being idle. If you want to work on that @pascalduez, feel free to reopen an issue with more details

@vvo vvo closed this as completed Jun 7, 2017
@lightlii
Copy link

lightlii commented Feb 8, 2018

@pascalduez did you have any success with this?
Similar case: I'm also working on a styleguide at my company and would like to use this to render code examples but because we use React-intl the output string is incorrect.

reactElementToJSXString(<Text tag="h1" size={Metrics.title} message="Title" />)

Expected output
screen shot 2018-02-08 at 14 24 19

observed output
screen shot 2018-02-08 at 14 24 38

thanks in advance

@pascalduez
Copy link
Contributor Author

@bnjmn-jns This looks like a different issue your describing, more to do with HOC's and displayName.
Quick fix Text.displayName = 'Text';

More thorough:
https://github.com/algolia/react-element-to-jsx-string#reactelementtojsxstringreactelement-options
https://github.com/algolia/react-element-to-jsx-string/blob/master/src/parser/parseReactElement.js#L12-L17

@lightlii
Copy link

lightlii commented Feb 8, 2018

@pascalduez works like a charm thank you!
and thanks for this library!

@ruyasan
Copy link

ruyasan commented Jun 1, 2018

What about support for function children in the element being rendered.

E.g.

reactElementToJSXString(<Foo>{() => <Whatever />}</Foo>, {showFunctions: true, functionValue: fn => "~replaced function~")

// Expected:
<Foo>~replaced-function~</Foo>
// Actual:
<Foo />

Looking at the source, I believe this is an unintentional side-effect of React.Children.toArray - it will discard any function children as it goes (React.Children.toArray([() => "hi"]) //=> []).

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

No branches or pull requests

4 participants