-
Notifications
You must be signed in to change notification settings - Fork 759
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test case for re-mounting components without reloading the page
- Loading branch information
Showing
14 changed files
with
277 additions
and
132 deletions.
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
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 @@ | ||
class CountersController < ApplicationController | ||
def index | ||
@counters = [{ name: "Counter 1" }] | ||
end | ||
|
||
def create | ||
@counter = { name: "Counter 2" } | ||
end | ||
end |
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,23 @@ | ||
var React = require("react"); | ||
var createReactClass = require("create-react-class"); | ||
|
||
module.exports = createReactClass({ | ||
getInitialState: function () { | ||
return { count: 0 }; | ||
}, | ||
handleClick: function () { | ||
this.setState({ count: this.state.count + 1 }); | ||
}, | ||
render: function () { | ||
return ( | ||
<div> | ||
<p> | ||
{this.props.name} - {this.state.count} | ||
</p> | ||
<button type="button" onClick={this.handleClick}> | ||
Increment {this.props.name} | ||
</button> | ||
</div> | ||
); | ||
}, | ||
}); |
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,26 @@ | ||
var { Controller } = require("@hotwired/stimulus"); | ||
var ReactRailsUJS = require("react_ujs"); | ||
|
||
module.exports = class extends Controller { | ||
connect() { | ||
this.observeChange(); | ||
} | ||
|
||
disconnect() { | ||
this.observer.disconnect(); | ||
} | ||
|
||
observeChange() { | ||
var element = this.element; | ||
var callback = function (mutationsList, _observer) { | ||
mutationsList.forEach(function (mutation) { | ||
if (mutation.type === "childList") { | ||
ReactRailsUJS.mountComponents(element); | ||
} | ||
}); | ||
}; | ||
|
||
this.observer = new MutationObserver(callback); | ||
this.observer.observe(this.element, { childList: true }); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,8 +1,15 @@ | ||
var ctx = require.context("components", true) | ||
var ReactRailsUJS = require("react_ujs") | ||
ReactRailsUJS.useContext(ctx) | ||
var React = require("react") | ||
require("@hotwired/turbo-rails"); | ||
var { Application } = require("@hotwired/stimulus"); | ||
var MountCountersController = require("../controllers/mount_counters"); | ||
|
||
window.GlobalComponent = function(props) { | ||
return React.createElement("h1", null, "Global Component") | ||
} | ||
window.Stimulus = Application.start(); | ||
Stimulus.register("mount-counters", MountCountersController); | ||
|
||
var ctx = require.context("components", true); | ||
var ReactRailsUJS = require("react_ujs"); | ||
ReactRailsUJS.useContext(ctx); | ||
var React = require("react"); | ||
|
||
window.GlobalComponent = function (props) { | ||
return React.createElement("h1", null, "Global Component"); | ||
}; |
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,3 @@ | ||
<%= turbo_stream.append :counters do %> | ||
<%= react_component("Counter", @counter) %> | ||
<% end %> |
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,10 @@ | ||
<h2>React 18 bug reproduction</h2> | ||
|
||
<%= turbo_frame_tag :counters, data: { controller: "mount-counters" } do %> | ||
<% @counters.each do |counter| %> | ||
<%= react_component("Counter", counter) %> | ||
<% end %> | ||
<% end %> | ||
<%= form_with(url: counters_path, method: :post, data: { turbo: true, turbo_stream: true }) do |form| %> | ||
<%= form.submit "Add counter" %> | ||
<% end %> |
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
Oops, something went wrong.