-
-
Notifications
You must be signed in to change notification settings - Fork 631
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
Doc/xhr hydration examples #1095
Merged
justin808
merged 12 commits into
shakacode:master
from
hchevalier:doc/xhr_hydration_examples
Jun 1, 2018
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
629435f
Document hydration for XHR-substituted components
hchevalier 2cd62b8
Update README.md
hchevalier 2a89b41
Fix rubocop offenses
hchevalier 7f6d547
Update CHANGELOG.md
hchevalier bff7bfb
Add PR reference to CHANGELOG.md
hchevalier 788bc8a
Add missing route to dummy_no_webpacker
hchevalier f9cb0b4
Refactor code according to review feedbacks
hchevalier 3737f92
Stop using sprocket jquery
hchevalier d0f029c
Remove unused piece of code
hchevalier a23d314
Fix eslint offenses on HelloWorldRehydratable
hchevalier ffdb42e
Refactor code according to review feedbacks
hchevalier f33fda0
Remove unused aliased jquery exposition
hchevalier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
//= require jquery | ||
//= require jquery_ujs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<%= react_component('HelloWorld', props: { helloWorldData: { name: 'HelloWorld' } }, | ||
prerender: true, | ||
trace: true, | ||
id: "HelloWorld-react-component-0") %> | ||
|
||
<%= react_component('HelloWorldRehydratable', props: { helloWorldData: { name: 'HelloWorldRehydratable' } }, | ||
prerender: true, | ||
trace: true, | ||
id: 'HelloWorldRehydratable-react-component-1') %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<% provide :other_javascript_tags do %> | ||
<%= javascript_include_tag 'jquery_rails_manifest', defer: true %> | ||
<% end %> | ||
|
||
<div id='component-container'> | ||
<%= render partial: 'xhr_refresh_partial' %> | ||
</div> | ||
|
||
<div> | ||
Click to refresh components through XHR (first component event handlers won't work anymore)<br/> | ||
<%= form_tag '/xhr_refresh', method: :get, remote: true, format: :js do %> | ||
<%= submit_tag 'Refresh', id: 'refresh', name: 'refresh' %> | ||
<% end %> | ||
</div> | ||
<hr/> | ||
|
||
<h1>React Rails Client Rehydration</h1> | ||
<p> | ||
This example demonstrates client side manual rehydration after a component replacement through XHR.<br/><br/> | ||
|
||
The "Refresh" button on this page will trigger an asynchrounous refresh of component-container content.<br/> | ||
Components will be prerendered by the server and inserted in the DOM (spec/dummy/app/views/pages/xhr_refresh.js.erb)<br/> | ||
No client rehydration will occur, preventing any event handler to be correctly attached<br/><br/> | ||
|
||
Thus, the onChange handler of the HelloWorld component won't trigger whereas the one from HellowWorldRehydratable will, thanks to the "hydrate" javscript event dispacthed from xhr_refresh.js.erb<br /> | ||
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. Typo on "javascript" |
||
</p> | ||
|
||
<hr/> | ||
<h2>Setup</h2> | ||
<ol> | ||
<li> | ||
Create component source: spec/dummy/client/app/components/HelloWorldHydratable.jsx | ||
</li> | ||
<li> | ||
Expose the HelloWorldHydratable Component: spec/dummy/client/app/startup/serverRegistration.jsx and spec/dummy/client/app/startup/clientRegistration.jsx | ||
<br/> | ||
<pre style='white-space: pre-wrap; word-break: keep-all;'> | ||
import HelloWorldHydratable from '../components/HelloWorldHydratable'; | ||
import ReactOnRails from 'react-on-rails'; | ||
ReactOnRails.register({ HelloWorldHydratable }); | ||
</pre> | ||
</li> | ||
<li> | ||
Place the component on the view: spec/dummy/app/views/pages/xhr_refresh.html.erb, making sure it has a parent node easily selectable | ||
<br/> | ||
<pre style='white-space: pre-wrap; word-break: keep-all;'> | ||
<div id='my-component-container'> | ||
<%%= react_component("HelloWorldHydratable", props: { helloWorldData: { name: 'HelloWorld' } }, prerender: true, trace: true, id: "HelloWorldHydratable-react-component-0") %> | ||
</div> | ||
</pre> | ||
</li> | ||
<li> | ||
Have a remote form allow to get xhr_request.js.erb | ||
<br/> | ||
<pre style='white-space: pre-wrap; word-break: keep-all;'> | ||
<%%= form_tag '/xhr_refresh', method: :get, remote: true, format: :js do %> | ||
<%%= submit_tag 'Refresh' %> | ||
<%% end %> | ||
</pre> | ||
</li> | ||
<li> | ||
In your xhr_request.js.erb, replace your container content and dispatch the 'hydrate' event that will be caught by HelloWorldHydratable event handler | ||
<br/> | ||
<pre style='white-space: pre-wrap; word-break: keep-all;'> | ||
var container = document.getElementById('component-container'); | ||
<%% new_component = react_component("HelloWorldHydratable", props: { helloWorldData: { name: 'HelloWorld' } }, prerender: true, trace: true, id: "HelloWorldHydratable-react-component-0") %> | ||
container.innerHTML = "<%%= escape_javascript(new_component) %>"; | ||
|
||
var event = document.createEvent('Event'); | ||
event.initEvent('hydrate', true, true); | ||
document.dispatchEvent(event); | ||
</pre> | ||
</li> | ||
</ol> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var container = document.getElementById('component-container'); | ||
container.innerHTML = "<%= escape_javascript(render partial: 'xhr_refresh_partial') %>"; | ||
|
||
var event = document.createEvent('Event'); | ||
event.initEvent('hydrate', true, true); | ||
document.dispatchEvent(event); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
spec/dummy/client/app/components/HelloWorldRehydratable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import ReactOnRails from 'react-on-rails'; | ||
import RailsContext from './RailsContext'; | ||
|
||
class HelloWorldRehydratable extends React.Component { | ||
|
||
static propTypes = { | ||
helloWorldData: PropTypes.shape({ | ||
name: PropTypes.string, | ||
}).isRequired, | ||
railsContext: PropTypes.object, | ||
}; | ||
|
||
// Not necessary if we only call super, but we'll need to initialize state, etc. | ||
constructor(props) { | ||
super(props); | ||
this.state = props.helloWorldData; | ||
this.setNameDomRef = this.setNameDomRef.bind(this); | ||
this.handleChange = this.handleChange.bind(this); | ||
this.forceClientHydration = this.forceClientHydration.bind(this) | ||
} | ||
|
||
componentDidMount() { | ||
document.addEventListener('hydrate', this.forceClientHydration); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener('hydrate', this.forceClientHydration); | ||
} | ||
|
||
forceClientHydration() { | ||
const registeredComponentName = 'HelloWorldRehydratable'; | ||
const { railsContext } = this.props; | ||
|
||
// Target all instances of the component in the DOM | ||
const match = document.querySelectorAll(`[id^=${registeredComponentName}-react-component-]`); | ||
// Not all browsers support forEach on NodeList so we go with a classic for-loop | ||
for (let i = 0; i < match.length; ++i) { | ||
const component = match[i]; | ||
|
||
// Get component specification <script> tag | ||
let domNode = component; | ||
while (domNode && !domNode.classList.contains('js-react-on-rails-component')) { | ||
// Before ReactOnRails v11.0.7, component specifications where inserted before the actual component | ||
// See https://github.com/shakacode/react_on_rails/commit/912118445f55c6f59664bf2b9f9ed97779ee71c9 | ||
// You may have to replace "nextElementSibling" by "previousElementSibling" if you use an older version | ||
domNode = domNode.nextElementSibling; | ||
} | ||
|
||
if (domNode) { | ||
// Read props from the component specification tag and merge railsContext | ||
const mergedProps = {...JSON.parse(domNode.textContent), railsContext}; | ||
// Hydrate | ||
ReactOnRails.render(registeredComponentName, mergedProps, component.id); | ||
} | ||
} | ||
} | ||
|
||
setNameDomRef(nameDomNode) { | ||
this.nameDomRef = nameDomNode; | ||
} | ||
|
||
handleChange() { | ||
const name = this.nameDomRef.value; | ||
this.setState({ name }); | ||
} | ||
|
||
render() { | ||
const { name } = this.state; | ||
const { railsContext } = this.props; | ||
|
||
return ( | ||
<div> | ||
<h3> | ||
Hello, {name}! | ||
</h3> | ||
<p> | ||
Say hello to: | ||
<input | ||
type="text" | ||
ref={this.setNameDomRef} | ||
defaultValue={name} | ||
onChange={this.handleChange} | ||
/> | ||
</p> | ||
{ railsContext && <RailsContext {...{ railsContext }} /> } | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default HelloWorldRehydratable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Typo on "asynchronous"