Skip to content

Commit 7de4f08

Browse files
author
Adam Comella
committed
Enable BackButton to rerender page on click
When the BackButton was clicked and caused a rerender, it could sometimes result in bad behavior such as an exception or an unexpected element receiving a click event. This is due to facebook/react#3790. This change adds a workaround for that bug to the BackButton.
1 parent 6fbe97d commit 7de4f08

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

react-winjs.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
// itemTemplate={this.itemTemplate} />
4040
// - Provide SplitViewButton & SplitViewCommand components or wait for WinJS to provide
4141
// the corresponding controls?
42+
// - Think more about React bug: https://github.com/facebook/react/issues/3790
43+
// BackButton has to work around it. I wonder if other controls which often cause
44+
// navigation on "click" will also have to workaround it (e.g. NavBarCommand).
4245

4346
var React = require('react/addons');
4447

@@ -794,6 +797,7 @@ var PropHandlers = {
794797
function defineControl(controlName, options) {
795798
options = options || {};
796799
var winControlOptions = options.winControlOptions || {};
800+
var preCtorInit = options.preCtorInit || function () { };
797801
var propHandlers = options.propHandlers || {};
798802
var render = options.render || function (component) {
799803
return React.DOM.div();
@@ -809,6 +813,7 @@ function defineControl(controlName, options) {
809813
// Give propHandlers that implement preCtorInit the opportunity to run before
810814
// instantiating the winControl.
811815
var options = cloneObject(winControlOptions);
816+
preCtorInit(element, options, winjsComponent.data, displayName);
812817
Object.keys(props).forEach(function (propName) {
813818
var preCtorInit = propHandlers[propName] && propHandlers[propName].preCtorInit;
814819
if (preCtorInit) {
@@ -1094,6 +1099,13 @@ var ControlApis = updateWithDefaults({
10941099
"AppBar.FlyoutCommand": CommandSpecs.FlyoutCommand,
10951100
AutoSuggestBox: {},
10961101
BackButton: {
1102+
preCtorInit: function (element, options, data, displayName) {
1103+
element.addEventListener("click", function (eventObject) {
1104+
// Prevent React from seeing the "click" event to workaround this React
1105+
// bug: https://github.com/facebook/react/issues/3790
1106+
eventObject.stopPropagation();
1107+
});
1108+
},
10971109
render: function (component) {
10981110
return React.DOM.button();
10991111
}

0 commit comments

Comments
 (0)