-
-
Notifications
You must be signed in to change notification settings - Fork 463
[all components] Support lazy element in render prop
#3856
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
Changes from all commits
49dccc7
19c7a3d
842ea90
570f715
c5457e7
5250459
d1639ba
34e7d17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,12 @@ function useRenderElementProps< | |
| return outProps; | ||
| } | ||
|
|
||
| // The symbol React uses internally for lazy components | ||
| // https://github.com/facebook/react/blob/a0566250b210499b4c5677f5ac2eedbd71d51a1b/packages/shared/ReactSymbols.js#L31 | ||
| // | ||
| // TODO delete once https://github.com/facebook/react/issues/32392 is fixed | ||
| const REACT_LAZY_TYPE = Symbol.for('react.lazy'); | ||
|
|
||
| function evaluateRenderProp<T extends React.ElementType, S>( | ||
| element: IntrinsicTagName | undefined, | ||
| render: BaseUIComponentProps<T, S>['render'], | ||
|
|
@@ -116,7 +122,36 @@ function evaluateRenderProp<T extends React.ElementType, S>( | |
| } | ||
| const mergedProps = mergeProps(props, render.props); | ||
| mergedProps.ref = props.ref; | ||
| return React.cloneElement(render, mergedProps); | ||
|
|
||
| let newElement = render; | ||
|
|
||
| // Workaround for https://github.com/facebook/react/issues/32392 | ||
| // This works because the toArray() logic unwrap lazy element type in | ||
| // https://github.com/facebook/react/blob/a0566250b210499b4c5677f5ac2eedbd71d51a1b/packages/react/src/ReactChildren.js#L186 | ||
| if (newElement?.$$typeof === REACT_LAZY_TYPE) { | ||
| const children = React.Children.toArray(render); | ||
| newElement = children[0] as BaseUIComponentProps<T, S>['render']; | ||
| } | ||
|
|
||
| // There is a high number of indirections, the error message thrown by React.cloneElement() is | ||
| // hard to use for developers, this logic provides a better context. | ||
| // | ||
| // Our general guideline is to never change the control flow depending on the environment. | ||
| // However, React.cloneElement() throws if React.isValidElement() is false, | ||
| // so we can throw before with custom message. | ||
| if (process.env.NODE_ENV !== 'production') { | ||
| if (!React.isValidElement(newElement)) { | ||
| throw /* minify-error-disabled */ new Error( | ||
| [ | ||
| 'Base UI: The `render` prop was provided an invalid React element as `React.isValidElement(render)` is `false`.', | ||
| 'A valid React element must be provided to the `render` prop because it is cloned with props to replace the default element.', | ||
| 'https://base-ui.com/r/invalid-render-prop', | ||
| ].join('\n'), | ||
| ); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this error message be more helpful? e.g. why is it invalid, how to fix it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I tried to do something better, applying:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion, add error writing style in your |
||
|
|
||
| return React.cloneElement(newElement, mergedProps); | ||
| } | ||
| if (element) { | ||
| if (typeof element === 'string') { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just remove that
[].join('\n')?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to minify the error, it won't run in production.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but why not write error messages according to the same rules, regardless of whether there's a production guard around? And why does it matter that this error does not get minified?
Anyway, we can probably automate this check like in mui/mui-public#794
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point
We still need
/* minify-error-disabled */to ignore that error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking care of non-prod errors in mui/mui-public#1069