Skip to content

Commit cd513c8

Browse files
committed
feat(ujs) rename loadContext -> useContext, add global fallback
1 parent 88a90af commit cd513c8

File tree

10 files changed

+39
-12
lines changed

10 files changed

+39
-12
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ The component name tells `react-rails` where to load the component. For example:
6565

6666
This way, you can access top-level, default, or named exports.
6767

68-
The `require.context` inserted into `packs/application.js` is used to load components. If you want to load components from a different directory, override it by calling `ReactRailsUJS.loadContext`:
68+
The `require.context` inserted into `packs/application.js` is used to load components. If you want to load components from a different directory, override it by calling `ReactRailsUJS.useContext`:
6969

7070
```js
7171
var myCustomContext = require.context("custom_components", true)
7272
var ReactRailsUJS = require("react_ujs")
7373
// use `custom_components/` for <%= react_component(...) %> calls
74-
ReactRailsUJS.loadContext(myCustomContext)
74+
ReactRailsUJS.useContext(myCustomContext)
7575
```
7676

77-
Alternatively, you can bypass `ReactRailsUJS.loadContext` altogether and use the global namespace approach described in [Use with Asset Pipeline](#use-with-asset-pipeline)
77+
Alternatively, you can bypass `ReactRailsUJS.useContext` altogether and use the global namespace approach described in [Use with Asset Pipeline](#use-with-asset-pipeline)
7878

7979
## Use with Asset Pipeline
8080

lib/generators/react/install_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def setup_react_sprockets
9999
// Support component names relative to this directory:
100100
var componentRequireContext = require.context("components", true)
101101
var ReactRailsUJS = require("react_ujs")
102-
ReactRailsUJS.loadContext(componentRequireContext)
102+
ReactRailsUJS.useContext(componentRequireContext)
103103
JS
104104

105105
def setup_react_webpacker

lib/generators/templates/server_rendering_pack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
// It must expose react_ujs as `ReactRailsUJS` and prepare a require context.
33
var componentRequireContext = require.context("components", true)
44
var ReactRailsUJS = require("react_ujs")
5-
ReactRailsUJS.loadContext(componentRequireContext)
5+
ReactRailsUJS.useContext(componentRequireContext)

react_ujs/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var ReactDOMServer = require("react-dom/server")
44

55
var detectEvents = require("./src/events/detect")
66
var constructorFromGlobal = require("./src/getConstructor/fromGlobal")
7-
var constructorFromRequireContext = require("./src/getConstructor/fromRequireContext")
7+
var constructorFromRequireContextWithGlobalFallback = require("./src/getConstructor/fromRequireContextWithGlobalFallback")
88

99
var ReactRailsUJS = {
1010
// This attribute holds the name of component which should be mounted
@@ -55,8 +55,8 @@ var ReactRailsUJS = {
5555
// the default is ReactRailsUJS.ComponentGlobal
5656
getConstructor: constructorFromGlobal,
5757

58-
loadContext: function(req) {
59-
this.getConstructor = constructorFromRequireContext(req)
58+
useContext: function(req) {
59+
this.getConstructor = constructorFromRequireContextWithGlobalFallback(req)
6060
},
6161

6262
// Render `componentName` with `props` to a string,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Make a function which:
2+
// - First tries to require the name
3+
// - Then falls back to global lookup
4+
var fromGlobal = require("./fromGlobal")
5+
var fromRequireContext = require("./fromRequireContext")
6+
7+
module.exports = function(reqctx) {
8+
var fromCtx = fromRequireContext(reqctx)
9+
return function(className) {
10+
var component;
11+
try {
12+
// `require` will raise an error if this className isn't found:
13+
component = fromCtx(className)
14+
} catch (err) {
15+
// fallback to global:
16+
component = fromGlobal(className)
17+
}
18+
return component
19+
}
20+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
var ctx = require.context("components", true)
22
var ReactRailsUJS = require("../../../../../react_ujs/index")
3-
ReactRailsUJS.loadContext(ctx)
3+
ReactRailsUJS.useContext(ctx)
4+
var React = require("react")
5+
6+
window.GlobalComponent = function(props) {
7+
return React.createElement("h1", null, "Global Component")
8+
}

test/dummy/app/javascript/packs/server_rendering.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
// It must expose react_ujs as `ReactRailsUJS` and prepare a require context.
33
var componentRequireContext = require.context("components", true)
44
var ReactRailsUJS = require("../../../../../react_ujs/index")
5-
ReactRailsUJS.loadContext(componentRequireContext)
5+
ReactRailsUJS.useContext(componentRequireContext)

test/dummy/app/views/pack_components/show.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
<%= react_component("export_default_component") %>
55
<%= react_component("named_export_component.Component") %>
66
<%= react_component("subfolder/exports_component") %>
7+
<%= react_component("GlobalComponent") %>

test/react/rails/webpacker_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ReactRailsWebpackerTest < ActionDispatch::IntegrationTest
1818
assert page.has_content?('Export Default')
1919
assert page.has_content?('Named Export')
2020
assert page.has_content?('Exports')
21+
assert page.has_content?('Global Component')
2122
end
2223
end
2324
end

test/react/server_rendering/webpacker_manifest_container_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class WebpackerManifestContainerTest < ActiveSupport::TestCase
1212
container = React::ServerRendering::WebpackerManifestContainer.new
1313
js_file = container.find_asset("application.js")
1414
# Main file:
15-
assert_includes js_file, "ReactRailsUJS.loadContext(ctx)"
15+
assert_includes js_file, "ReactRailsUJS.useContext(ctx)"
1616
# Bundled dependencies:
1717
assert_includes js_file, "ExportDefaultComponent"
1818
end
@@ -22,7 +22,7 @@ def test_it_loads_from_webpack_dev_server
2222
container = React::ServerRendering::WebpackerManifestContainer.new
2323
js_file = container.find_asset("application.js")
2424
# Main file:
25-
assert_includes js_file, "ReactRailsUJS.loadContext(ctx)"
25+
assert_includes js_file, "ReactRailsUJS.useContext(ctx)"
2626
# Bundled dependencies:
2727
assert_includes js_file, "ExportDefaultComponent"
2828
end

0 commit comments

Comments
 (0)