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

feat(formatting): render children as function #338

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

adammockor
Copy link

@adammockor adammockor commented Jan 4, 2019

This is copy paste of #317 since it doesn't get merged due commitlint errors. I fixed that. Thanks @DavidBabel for implementing this. I can provide support to this PR if needed.

@adammockor
Copy link
Author

I will fix the flow errors. But failing test is not related to this PR and local yarn run test works without error.

@vvo
Copy link
Contributor

vvo commented Jan 4, 2019

Yep I don't get why it's failing on travis only
image

@vvo
Copy link
Contributor

vvo commented Jan 4, 2019

cc @Spy-Seth maybe?

@DavidBabel
Copy link

Do you use the same node version as your travis build ? node_js: node # lastest

@armandabric
Copy link
Collaborator

The ref/key order issue is fixed by #340. You should rebase your PR on top master 😃

Copy link
Collaborator

@armandabric armandabric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adammockor for this contribution. This feature is really missing in react-element-to-jsx-string

For now, I have some concern about the current proposal to implement it. It's a good start to implement the feature 😺

@@ -71,6 +72,14 @@ const parseReactElement = (
.filter(onlyMeaningfulChildren)
.map(child => parseReactElement(child, options));

if (typeof element.props.children === 'function') {
const functionChildrens = parseReactElement(
element.props.children(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about the need to call the children function to be able to format it's result. This could have unwanted side effect in the user land.

For example:

reactElementToJSXString(
  <div>
    {() => {
      console.log('Should not be logged');

      return <div>Hello World</div>;
    }}
  </div>,
  {
    showFunctions: true,
  }
)

);
childrens.push(createReactFunctionTreeNode([functionChildrens]));
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One gotcha with this detection method is is case of mixed children (or multiple function as a children):

// Multiple functions as child
reactElementToJSXString(
  <div>
    {() => <div>Hello Foo</div>}
    {() => <div>Hello Bar</div>}
  </div>,
  {
    showFunctions: true,
  }
)

// Mixed content
reactElementToJSXString(
  <div>
    {() => <div>Hello Foo</div>}
    <span>Some other children</span>
  </div>,
  {
    showFunctions: true,
  }
)

I know this is should not be frequent or maybe event imaginable but the JSX allow it 🤷‍♂️

I do some try with you PR, it's a shame that React.Children filters out the function children. We do not want to re-implement it to be able to conserve function in our parsing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and this is related to React.Children behavior.

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.

None yet

4 participants