Skip to content

Switch to named imports #1730

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/javascript-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Rails has built-in protection for Cross-Site Request Forgery (CSRF), see [Rails Documentation](http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf). To nicely utilize this feature in JavaScript requests, React on Rails provides two helpers that can be used as following for POST, PUT or DELETE requests:

```js
import ReactOnRails from 'react-on-rails';
import * as ReactOnRails from 'react-on-rails';

// reads from DOM csrf token generated by Rails in <%= csrf_meta_tags %>
csrfToken = ReactOnRails.authenticityToken();
Expand Down
8 changes: 4 additions & 4 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ issue.
- The `component_name` parameter is a string matching the name you used to expose your React component globally. So, in the above examples, if you had a React component named "HelloWorld", you would register it with the following lines:

```js
import ReactOnRails from 'react-on-rails';
import { register } from 'react-on-rails';
import HelloWorld from './HelloWorld';
ReactOnRails.register({ HelloWorld });
register({ HelloWorld });
```

Exposing your component in this way allows you to reference the component from a Rails view. You can expose as many components as you like, but their names must be unique. See below for the details of how you expose your components via the React on Rails Webpack configuration. You may call `ReactOnRails.register` many times.
Expand Down Expand Up @@ -133,8 +133,8 @@ This is how to expose a component to the `react_component` view helper.
```javascript
// app/javascript/packs/hello-world-bundle.js
import HelloWorld from '../components/HelloWorld';
import ReactOnRails from 'react-on-rails';
ReactOnRails.register({ HelloWorld });
import { register } from 'react-on-rails';
register({ HelloWorld });
```

#### Different Server-Side Rendering Code (and a Server-Specific Bundle)
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/file-system-based-automated-bundle-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ app/javascript:
└── logo.svg
```

Previously, many applications would use one pack (webpack entrypoint) for many components. In this example, the`application.js` file manually registers server components, `FooComponentOne`, `BarComponentOne` and `BarComponentTwo`.
Previously, many applications would use one pack (webpack entrypoint) for many components. In this example, the `application.js` file manually registers server components, `FooComponentOne`, `BarComponentOne` and `BarComponentTwo`.

```jsx
import ReactOnRails from 'react-on-rails';
import { register } from 'react-on-rails';
import FooComponentOne from '../src/Foo/FooComponentOne';
import BarComponentOne from '../src/Foo/BarComponentOne';
import BarComponentTwo from '../src/Foo/BarComponentTwo';

ReactOnRails.register({ FooComponentOne, BarComponentOne, BarComponentTwo });
register({ FooComponentOne, BarComponentOne, BarComponentTwo });
```

Your layout would contain:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ Many projects will have different entry points for client and server rendering.
Your Client Entry can look like this:

```js
import ReactOnRails from 'react-on-rails/client';
import { register } from 'react-on-rails/client';
import App from './ClientApp';
ReactOnRails.register({ App });
register({ App });
```

So your Server Entry can look like:

```js
import ReactOnRails from 'react-on-rails';
import { register } from 'react-on-rails';
import App from './ServerApp';
ReactOnRails.register({ App });
register({ App });
```

Note that the only difference is in the imports.
Expand Down
6 changes: 3 additions & 3 deletions docs/javascript/code-splitting.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Here's an example of how you might use this in practice:
#### clientRegistration.js

```js
import ReactOnRails from 'react-on-rails/client';
import * as ReactOnRails from 'react-on-rails/client';
import NavigationApp from './NavigationApp';

// Note that we're importing a different RouterApp than in serverRegistration.js
Expand All @@ -57,7 +57,7 @@ ReactOnRails.register({
#### serverRegistration.js

```js
import ReactOnRails from 'react-on-rails';
import * as ReactOnRails from 'react-on-rails';
import NavigationApp from './NavigationApp';

// Note that we're importing a different RouterApp than in clientRegistration.js
Expand All @@ -76,7 +76,7 @@ Note that you should not register a renderer on the server, since there won't be
#### RouterAppRenderer.jsx

```jsx
import ReactOnRails from 'react-on-rails/client';
import * as ReactOnRails from 'react-on-rails/client';
import React from 'react';
import ReactDOM from 'react-dom';
import Router from 'react-router/lib/Router';
Expand Down
2 changes: 1 addition & 1 deletion docs/javascript/react-router.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import React from 'react';
import { renderToString } from 'react-dom/server';
import { StaticRouter } from 'react-router';
import { Provider } from 'react-redux';
import ReactOnRails from 'react-on-rails';
import * as ReactOnRails from 'react-on-rails';

// App.jsx from src/client/App.jsx
import App from '../App';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ReactOnRails from 'react-on-rails/client';
import { register } from 'react-on-rails/client';

import <%= config[:component_name] %> from '<%= config[:app_relative_path] %>';

// This is how react_on_rails can see the HelloWorld in the browser.
ReactOnRails.register({
register({
<%= config[:component_name] %>,
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ReactOnRails from 'react-on-rails';
import { register } from 'react-on-rails';

import HelloWorld from '../bundles/HelloWorld/components/HelloWorldServer';

// This is how react_on_rails can see the HelloWorld in the browser.
ReactOnRails.register({
register({
HelloWorld,
});
5 changes: 3 additions & 2 deletions lib/react_on_rails/packs_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def pack_file_contents(file_path)
relative_component_path = relative_component_path_from_generated_pack(file_path)

<<~FILE_CONTENT.strip
import ReactOnRails from 'react-on-rails/client';
import * as ReactOnRails from 'react-on-rails/client';
import #{registered_component_name} from '#{relative_component_path}';

ReactOnRails.register({#{registered_component_name}});
Expand All @@ -123,7 +123,8 @@ def create_server_pack

def build_server_pack_content(component_on_server_imports, server_components, client_components)
content = <<~FILE_CONTENT
import ReactOnRails from 'react-on-rails';
import * as ReactOnRails from 'react-on-rails';
globalThis.ReactOnRails = ReactOnRails;

#{component_on_server_imports.join("\n")}\n
FILE_CONTENT
Expand Down
Loading
Loading