Skip to content

Commit 99ded57

Browse files
RiccardoMargiottacymen
authored andcommitted
Update README to include information about useContexts.
1 parent 2c7ca31 commit 99ded57

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,21 @@ ReactRailsUJS.useContext(myCustomContext)
172172

173173
If `require` fails to find your component, [`ReactRailsUJS`](#ujs) falls back to the global namespace, described in [Use with Asset Pipeline](#use-with-asset-pipeline).
174174

175-
In some cases, having multiple `require.context` may be desired. If so, an array of `require.context` can be passed to `ReactRailsUJS.useContexts`. With an array of contexts, an attempt will be made to `require` the component from each context before falling back to the global namespace as described above.
175+
In some cases, having multiple `require.context` may be desired - for example, if you have additional entry points to create JavaScript files for different routes, you would add multiple `require.context` to your `server_rendering.js` to allow for [Server-Side Rendering](#server-side-rendering) across your application. If so, an array of `require.context` can be passed to `ReactRailsUJS.useContexts`. With an array of contexts, an attempt will be made to `require` the component from each context before falling back to the global namespace as described above.
176176

177+
```js
178+
// server_rendering.js
179+
var componentsRequireContext = require.context('components', true);
180+
var searchRequireContext = require.context('search', true);
181+
var checkoutRequireContext = require.context('checkout', true);
182+
183+
var ReactRailsUJS = require('react_ujs');
184+
ReactRailsUJS.useContexts([
185+
componentsRequireContext,
186+
searchRequireContext,
187+
checkoutRequireContext
188+
]);
189+
```
177190
### File naming
178191

179192
React-Rails supports plenty of file extensions such as: .js, .jsx.js, .js.jsx, .es6.js, .coffee, etcetera!

0 commit comments

Comments
 (0)