-
Notifications
You must be signed in to change notification settings - Fork 751
add render component extension point #1064
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
ee3557f
7cb3d15
acdee60
6543967
bfe0549
ad27aee
fffc5e8
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,8 @@ var detectEvents = require("./src/events/detect") | |||||||||
| var constructorFromGlobal = require("./src/getConstructor/fromGlobal") | ||||||||||
| var constructorFromRequireContextWithGlobalFallback = require("./src/getConstructor/fromRequireContextWithGlobalFallback") | ||||||||||
|
|
||||||||||
| var renderWithReactDOM = require("./src/renderComponent/withReactDOM") | ||||||||||
|
|
||||||||||
| var ReactRailsUJS = { | ||||||||||
| // This attribute holds the name of component which should be mounted | ||||||||||
| // example: `data-react-class="MyApp.Items.EditForm"` | ||||||||||
|
|
@@ -71,6 +73,11 @@ var ReactRailsUJS = { | |||||||||
| useContext: function(requireContext) { | ||||||||||
| this.getConstructor = constructorFromRequireContextWithGlobalFallback(requireContext) | ||||||||||
| }, | ||||||||||
|
|
||||||||||
| // Called after React unmounts component at `node` | ||||||||||
| // Override this function to perform any cleanup | ||||||||||
| // the default function does nothing | ||||||||||
| onComponentUnmountAtNode: function (node) {}, | ||||||||||
|
|
||||||||||
| // Render `componentName` with `props` to a string, | ||||||||||
| // using the specified `renderFunction` from `react-dom/server`. | ||||||||||
|
|
@@ -80,6 +87,11 @@ var ReactRailsUJS = { | |||||||||
| return ReactDOMServer[renderFunction](element) | ||||||||||
| }, | ||||||||||
|
|
||||||||||
| // Render `component` using the specified `renderFunction` from `react-dom`. | ||||||||||
| // Override this function to render components in a custom way. | ||||||||||
| // function signature: ("hydrate" | "render", component, node, props) | ||||||||||
| renderComponent: renderWithReactDOM, | ||||||||||
|
Comment on lines
+90
to
+93
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. The comment and pattern here intends to mirror
Reference: react-rails/react_ujs/index.js Lines 63 to 66 in d5da111
|
||||||||||
|
|
||||||||||
| // Within `searchSelector`, find nodes which should have React components | ||||||||||
| // inside them, and mount them with their props. | ||||||||||
| mountComponents: function(searchSelector) { | ||||||||||
|
|
@@ -112,9 +124,9 @@ var ReactRailsUJS = { | |||||||||
| } | ||||||||||
|
|
||||||||||
| if (hydrate && typeof ReactDOM.hydrate === "function") { | ||||||||||
| component = ReactDOM.hydrate(component, node); | ||||||||||
| renderComponent("hydrate", component, node, props); | ||||||||||
| } else { | ||||||||||
| component = ReactDOM.render(component, node); | ||||||||||
| renderComponent("render", component, node, props); | ||||||||||
| } | ||||||||||
|
Comment on lines
126
to
130
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. Assignment to
|
||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -128,6 +140,7 @@ var ReactRailsUJS = { | |||||||||
| for (var i = 0; i < nodes.length; ++i) { | ||||||||||
| var node = nodes[i]; | ||||||||||
| ReactDOM.unmountComponentAtNode(node); | ||||||||||
| ReactRailsUJS.onComponentUnmountAtNode(node); | ||||||||||
| } | ||||||||||
| }, | ||||||||||
|
|
||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| var ReactDOM = require("react-dom") | ||
|
|
||
| // Render React component via ReactDOM, for example: | ||
| // | ||
| // - `renderComponent("hydrate", component, node, props)` -> `ReactDOM.hydrate(component, node);` | ||
| // - `renderComponent("render", component, node, props)` -> `ReactDOM.render(component, node);` | ||
| // | ||
| module.exports = function(renderFunctionName, component, node) { | ||
| ReactDOM[renderFunctionName](component, node); | ||
| }; | ||
|
Comment on lines
+3
to
+10
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. The last argument Let me know if
Previous proposal referenced: #595 (comment)
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. Why
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. Considerations for removing Furthermore, existing implementation of turbolinks would likely be required to move here in such case; this increases the complexity to override the render function. The intention is to keep the render function as simple as possible such that it has minimal impact to existing features when it's overriden I do not have benchmarks/metrics to support a performance argument |
||
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.
folder structure and import pattern is taking reference from
getConstructorfor consistency