Skip to content

Commit 8496f3d

Browse files
author
Thomas Bolis
committed
fixing bug with event
improving example to not set state on each change
1 parent b5edf24 commit 8496f3d

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

examples/main.jsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ function eventFire(el, etype) {
7070
}
7171
}
7272

73+
import PureRenderMixin from 'react-addons-pure-render-mixin';
74+
7375
class SketchFieldDemo extends React.Component {
7476
constructor(params) {
7577
super(params);
@@ -83,20 +85,22 @@ class SketchFieldDemo extends React.Component {
8385
this._renderTile = this._renderTile.bind(this);
8486
this._selectTool = this._selectTool.bind(this);
8587

86-
this.state = {
87-
lineColor: 'black',
88-
lineWidth: 10,
89-
fillColor: '#68CCCA',
90-
shadowWidth: 0,
91-
shadowOffset: 0,
92-
tool: Tools.Pencil,
93-
fillWithColor: false,
94-
drawings: [],
95-
canUndo: false,
96-
canRedo: false
97-
};
88+
this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
89+
9890
}
9991

92+
state = {
93+
lineColor: 'black',
94+
lineWidth: 10,
95+
fillColor: '#68CCCA',
96+
shadowWidth: 0,
97+
shadowOffset: 0,
98+
tool: Tools.Pencil,
99+
fillWithColor: false,
100+
drawings: [],
101+
canUndo: false,
102+
canRedo: false
103+
};
100104

101105
componentDidMount() {
102106

@@ -257,9 +261,13 @@ class SketchFieldDemo extends React.Component {
257261
height={660}
258262
defaultData={dataJson}
259263
defaultDataType="json"
260-
onChange={(e) => {
261-
this.setState({canUndo: this._sketch.canUndo()})
262-
}}
264+
onChange={() => {
265+
let prev = this.state.canUndo;
266+
let now = this._sketch.canUndo();
267+
if(prev !== now){
268+
this.setState({canUndo: now});
269+
}
270+
}}
263271
tool={this.state.tool}
264272
/>
265273
</div>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
},
9595
"dependencies": {
9696
"react": "^15.1.0",
97-
"react-dom": "^15.1.0",
98-
"react-addons-pure-render-mixin": "^15.1.0"
97+
"react-addons-pure-render-mixin": "^15.1.0",
98+
"react-dom": "^15.1.0"
9999
}
100100
}

src/SketchField.jsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,20 @@ class SketchField extends Component {
215215

216216
_onMouseUp(e) {
217217
this._selectedTool.doMouseUp(e);
218-
let onChange = this.props.onChange;
219-
if (onChange) {
220-
setTimeout(()=> {
221-
onChange(event.e);
218+
if (this.props.onChange) {
219+
let onChange = this.props.onChange;
220+
setTimeout(() => {
221+
onChange(e.e);
222222
}, 10);
223223
}
224224
}
225225

226226
_onMouseOut(e) {
227227
this._selectedTool.doMouseOut(e);
228-
let onChange = this.props.onChange;
229-
if (onChange) {
230-
setTimeout(()=> {
231-
onChange(event.e);
228+
if (this.props.onChange) {
229+
let onChange = this.props.onChange;
230+
setTimeout(() => {
231+
onChange(e.e);
232232
}, 10);
233233
}
234234
}
@@ -300,7 +300,7 @@ class SketchField extends Component {
300300
this._fc.renderAll();
301301
}
302302
if (this.props.onChange) {
303-
this.props.onChange(event.e);
303+
this.props.onChange();
304304
}
305305
}
306306

@@ -325,7 +325,7 @@ class SketchField extends Component {
325325
obj.setCoords();
326326
canvas.renderAll();
327327
if (this.props.onChange) {
328-
this.props.onChange(event.e);
328+
this.props.onChange();
329329
}
330330
}
331331
}
@@ -396,7 +396,7 @@ class SketchField extends Component {
396396
canvas.loadFromJSON(json, () => {
397397
canvas.renderAll();
398398
if (this.props.onChange) {
399-
this.props.onChange(null);
399+
this.props.onChange();
400400
}
401401
});
402402
}, 100);
@@ -418,7 +418,7 @@ class SketchField extends Component {
418418
canvas.renderAll();
419419
};
420420
if (this.props.onChange) {
421-
this.props.onChange(null);
421+
this.props.onChange();
422422
}
423423
}
424424

0 commit comments

Comments
 (0)