diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cf322075..f17f61c1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ Changes since last non-beta release. *Please add entries here for your pull requests that are not yet released.* #### Changed +- Document how to manually rehydrate XHR-substituted components on client side. [PR 1095](https://github.com/shakacode/react_on_rails/pull/1095) by [hchevalier](https://github.com/hchevalier). + ### [11.0.7] - 2018-05-11 #### Fixed - Fix npm publshing. [PR 1090](https://github.com/shakacode/react_on_rails/pull/1090) by [justin808](https://github.com/justin808). diff --git a/README.md b/README.md index 529f48b8d..ed2aadba7 100644 --- a/README.md +++ b/README.md @@ -540,7 +540,10 @@ All options except `props, id, html_options` will inherit from your `react_on_ra + **replay_console:** Default is true. False will disable echoing server-rendering logs to the browser. While this can make troubleshooting server rendering difficult, so long as you have the configuration of `logging_on_server` set to true, you'll still see the errors on the server. + **logging_on_server:** Default is true. True will log JS console messages and errors to the server. + **raise_on_prerender_error:** Default is false. True will throw an error on the server side rendering. Your controller will have to handle the error. - + + Note: client hydration will not trigger for components rendered through XHR. You will have to handle it with javascript. + For an example, see [spec/dummy/app/views/pages/xhr_refresh.rb](https://github.com/shakacode/react_on_rails/tree/master/spec/dummy/app/views/pages/xhr_refresh.rb). + ### react_component_hash `react_component_hash` is used to return multiple HTML strings for server rendering, such as for adding meta-tags to a page. It is exactly like react_component except for the following: diff --git a/spec/dummy/app/views/pages/_xhr_refresh_partial.html.erb b/spec/dummy/app/views/pages/_xhr_refresh_partial.html.erb new file mode 100644 index 000000000..862aaf31d --- /dev/null +++ b/spec/dummy/app/views/pages/_xhr_refresh_partial.html.erb @@ -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') %> diff --git a/spec/dummy/app/views/pages/xhr_refresh.html.erb b/spec/dummy/app/views/pages/xhr_refresh.html.erb new file mode 100644 index 000000000..06096d9d7 --- /dev/null +++ b/spec/dummy/app/views/pages/xhr_refresh.html.erb @@ -0,0 +1,70 @@ +
+ This example demonstrates client side manual rehydration after a component replacement through XHR.
+
+ The "Refresh" button on this page will trigger an asynchronous refresh of component-container content.
+ Components will be prerendered by the server and inserted in the DOM (spec/dummy/app/views/pages/xhr_refresh.js.erb)
+ No client rehydration will occur, preventing any event handler to be correctly attached
+
+ Thus, the onChange handler of the HelloWorld component won't trigger whereas the one from HellowWorldRehydratable will, thanks to the "hydrate" javascript event dispacthed from xhr_refresh.js.erb
+
+ import HellowWorldRehydratable from '../components/HellowWorldRehydratable'; + import ReactOnRails from 'react-on-rails'; + ReactOnRails.register({ HellowWorldRehydratable }); ++
+++ <%%= react_component("HellowWorldRehydratable", props: { helloWorldData: { name: 'HelloWorld' } }, prerender: true, trace: true, id: "HellowWorldRehydratable-react-component-0") %> ++
+ <%%= form_tag '/xhr_refresh', method: :get, remote: true, format: :js do %> + <%%= submit_tag 'Refresh' %> + <%% end %> ++
+ var container = document.getElementById('component-container'); + <%% new_component = react_component("HellowWorldRehydratable", props: { helloWorldData: { name: 'HelloWorld' } }, prerender: true, trace: true, id: "HellowWorldRehydratable-react-component-0") %> + container.innerHTML = "<%%= escape_javascript(new_component) %>"; + + var event = document.createEvent('Event'); + event.initEvent('hydrate', true, true); + document.dispatchEvent(event); ++