Skip to content

Commit 2f01580

Browse files
committed
fix events can not be removed, fixed #151
1 parent b3bf43f commit 2f01580

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

__tests__/index.spec.jsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ describe('index.js', () => {
121121
const component = mount(<EchartsReact
122122
option={option}
123123
opts={{renderer: 'svg'}}
124-
125124
/>);
126125

127126
const preId = component.instance().getEchartsInstance().id;
@@ -142,6 +141,39 @@ describe('index.js', () => {
142141
expect(preId).not.toBe(component.instance().getEchartsInstance().id);
143142
});
144143

144+
// update opts, should dispose echarts instance.
145+
test('update onEvents', () => {
146+
const component = mount(<EchartsReact
147+
option={option}
148+
onEvents={{
149+
click: () => {},
150+
mousemove: () => {},
151+
}}
152+
/>);
153+
154+
const preId = component.instance().getEchartsInstance().id;
155+
// udpate props
156+
component.setProps({
157+
onEvents: {
158+
mousemove: () => {},
159+
click: () => {},
160+
}
161+
});
162+
163+
component.update(); // force update
164+
expect(preId).toBe(component.instance().getEchartsInstance().id);
165+
166+
// udpate props
167+
component.setProps({
168+
onEvents: {
169+
click: () => {},
170+
}
171+
});
172+
173+
component.update(); // force update
174+
expect(preId).not.toBe(component.instance().getEchartsInstance().id);
175+
});
176+
145177
test('unmount', () => {
146178
const component = mount(<EchartsReact
147179
option={option}

demo/dist/bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/core.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,21 @@ var EchartsReactCore = function (_Component) {
7474
};
7575

7676
_this.bindEvents = function (instance, events) {
77-
var _loopEvent = function _loopEvent(eventName) {
77+
var _bindEvent = function _bindEvent(eventName, func) {
7878
// ignore the event config which not satisfy
79-
if (typeof eventName === 'string' && typeof events[eventName] === 'function') {
79+
if (typeof eventName === 'string' && typeof func === 'function') {
8080
// binding event
81-
instance.off(eventName);
81+
// instance.off(eventName); // 已经 dispose 在重建,所以无需 off 操作
8282
instance.on(eventName, function (param) {
83-
events[eventName](param, instance);
83+
func(param, instance);
8484
});
8585
}
8686
};
8787

88+
// loop and bind
8889
for (var eventName in events) {
8990
if (Object.prototype.hasOwnProperty.call(events, eventName)) {
90-
_loopEvent(eventName);
91+
_bindEvent(eventName, events[eventName]);
9192
}
9293
}
9394
};
@@ -125,15 +126,18 @@ var EchartsReactCore = function (_Component) {
125126
var echartObj = this.renderEchartDom();
126127
this.bindEvents(echartObj, this.props.onEvents || {});
127128

128-
// 1. 切换 theme 的时候,需要 dispose 之后再新建
129-
// 2. 修改 opts 的时候,需要 dispose 之后再新建
130-
if (this.props.theme !== prevProps.theme || !isEqual(prevProps.opts, this.props.opts)) {
129+
// 以下属性修改的时候,需要 dispose 之后再新建
130+
// 1. 切换 theme 的时候
131+
// 2. 修改 opts 的时候
132+
// 3. 修改 onEvents 的时候,这样可以取消所以之前绑定的事件 issue #151
133+
if (prevProps.theme !== this.props.theme || !isEqual(prevProps.opts, this.props.opts) || !isEqual(Object.keys(prevProps.onEvents).sort(), Object.keys(this.props.onEvents).sort())) {
131134
this.dispose();
132135

133136
this.rerender(); // 重建
134137
return;
135138
}
136139

140+
// 样式修改的时候,可能会导致大小变化,所以触发一下 resize
137141
if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
138142
try {
139143
echartObj.resize();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "echarts-for-react",
3-
"version": "2.0.6",
3+
"version": "2.0.7",
44
"description": "baidu Echarts(v3.x & v4.x) components for react.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/core.jsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ export default class EchartsReactCore extends Component {
2121
const echartObj = this.renderEchartDom();
2222
this.bindEvents(echartObj, this.props.onEvents || {});
2323

24-
// 1. 切换 theme 的时候,需要 dispose 之后再新建
25-
// 2. 修改 opts 的时候,需要 dispose 之后再新建
26-
if (this.props.theme !== prevProps.theme || !isEqual(prevProps.opts, this.props.opts)) {
24+
// 以下属性修改的时候,需要 dispose 之后再新建
25+
// 1. 切换 theme 的时候
26+
// 2. 修改 opts 的时候
27+
// 3. 修改 onEvents 的时候,这样可以取消所以之前绑定的事件 issue #151
28+
if (
29+
prevProps.theme !== this.props.theme ||
30+
!isEqual(prevProps.opts, this.props.opts) ||
31+
!isEqual(Object.keys(prevProps.onEvents).sort(), Object.keys(this.props.onEvents).sort())
32+
) {
2733
this.dispose();
2834

2935
this.rerender(); // 重建
3036
return;
3137
}
3238

39+
// 样式修改的时候,可能会导致大小变化,所以触发一下 resize
3340
if (!isEqual(prevProps.style, this.props.style) || !isEqual(prevProps.className, this.props.className)) {
3441
try {
3542
echartObj.resize();
@@ -74,20 +81,21 @@ export default class EchartsReactCore extends Component {
7481

7582
// bind the events
7683
bindEvents = (instance, events) => {
77-
const _loopEvent = (eventName) => {
84+
const _bindEvent = (eventName, func) => {
7885
// ignore the event config which not satisfy
79-
if (typeof eventName === 'string' && typeof events[eventName] === 'function') {
86+
if (typeof eventName === 'string' && typeof func === 'function') {
8087
// binding event
81-
instance.off(eventName);
88+
// instance.off(eventName); // 已经 dispose 在重建,所以无需 off 操作
8289
instance.on(eventName, (param) => {
83-
events[eventName](param, instance);
90+
func(param, instance);
8491
});
8592
}
8693
};
8794

95+
// loop and bind
8896
for (const eventName in events) {
8997
if (Object.prototype.hasOwnProperty.call(events, eventName)) {
90-
_loopEvent(eventName);
98+
_bindEvent(eventName, events[eventName]);
9199
}
92100
}
93101
};

0 commit comments

Comments
 (0)