Skip to content

Commit ea7e615

Browse files
fixing handlers again
1 parent b71c464 commit ea7e615

File tree

6 files changed

+134
-5
lines changed

6 files changed

+134
-5
lines changed

dev/App.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React, {Component} from 'react';
2+
import {hot} from 'react-hot-loader';
3+
import createPlotComponent from '../src/factory';
4+
5+
const Plot = createPlotComponent(Plotly);
6+
7+
class App extends Component {
8+
9+
constructor(props){
10+
super(props);
11+
this.state = {
12+
y: [1],
13+
}
14+
15+
16+
this.data = {
17+
type: 'scatter',
18+
mode: 'points',
19+
marker: {color: 'blue'}
20+
}
21+
22+
this.limitData = {
23+
type: 'scatter',
24+
mode: 'line',
25+
x:[0,20],
26+
y:[2,2]
27+
}
28+
29+
this.layout = {
30+
dragmode: 'select',
31+
width: 640,
32+
height: 480
33+
}
34+
35+
this.addData = () => {
36+
this.setState(prevState => ({y: [...prevState.y, Math.random()]}));
37+
}
38+
39+
this.resetData = () => {
40+
this.setState({y: [1]})
41+
}
42+
43+
this.streamData = () => {
44+
const timer = setInterval(() => this.addData(), 1000);
45+
this.setState(prevState => ({...prevState, timer}));
46+
}
47+
48+
this.stopData = () => {
49+
clearInterval(this.state.timer);
50+
}
51+
}
52+
53+
54+
render(){
55+
return (
56+
<div>
57+
58+
<div>
59+
<button onClick={this.streamData}> Add data </button>
60+
<button onClick={this.stopData}> Stop </button>
61+
<button onClick={this.resetData}> Reset </button>
62+
</div>
63+
<Plot
64+
data={[{...this.data, y: this.state.y}, this.limitData]}
65+
layout={this.layout}
66+
onRestyle={this.state.y.length % 2 ? undefined: console.log}
67+
onRelayout={this.state.y.length % 2 ? undefined: console.log}
68+
onAfterPlot={this.state.y.length % 2 ? undefined: console.log}
69+
onAnimatingFrame={this.state.y.length % 2 ? undefined: console.log}
70+
onFramework={this.state.y.length % 2 ? undefined: console.log}
71+
onSliderChange={this.state.y.length % 2 ? undefined: console.log}
72+
/>
73+
</div>
74+
);
75+
}
76+
}
77+
78+
export default hot(module)(App);

dev/favicon.ico

3.78 KB
Binary file not shown.

dev/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<link rel="shortcut icon" href="/favicon.ico">
6+
<title>Dev</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
11+
<script src="bundle.js" type="text/javascript"></script>
12+
</body>
13+
</html>

dev/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
ReactDOM.render(<App />, document.getElementById('root'));

src/factory.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default function plotComponentFactory(Plotly) {
6363
this.attachUpdateEvents = this.attachUpdateEvents.bind(this);
6464
this.getRef = this.getRef.bind(this);
6565
this.handleUpdate = this.handleUpdate.bind(this);
66+
this.handleUpdateWithProps = this.handleUpdateWithProps.bind(this);
6667
}
6768

6869
shouldComponentUpdate(nextProps) {
@@ -73,7 +74,6 @@ export default function plotComponentFactory(Plotly) {
7374
}
7475

7576
componentDidMount() {
76-
if (!isBrowser) return;
7777
this.p = this.p
7878
.then(() => {
7979
return Plotly.newPlot(this.el, {
@@ -96,7 +96,6 @@ export default function plotComponentFactory(Plotly) {
9696
}
9797

9898
componentWillUpdate(nextProps) {
99-
if (!isBrowser) return;
10099
this.p = this.p
101100
.then(() => {
102101
if (hasReactAPIMethod) {
@@ -121,7 +120,7 @@ export default function plotComponentFactory(Plotly) {
121120
.then(() => {
122121
if (!hasReactAPIMethod) this.attachUpdateEvents();
123122
})
124-
.then(() => this.handleUpdate(nextProps))
123+
.then(() => this.handleUpdateWithProps(nextProps))
125124
.catch(err => {
126125
console.error('Error while plotting:', err);
127126
this.props.onError && this.props.onError(err);
@@ -162,8 +161,11 @@ export default function plotComponentFactory(Plotly) {
162161
}
163162
}
164163

165-
handleUpdate(props) {
166-
props = props || this.props;
164+
handleUpdate() {
165+
this.handleUpdateWithProps(this.props);
166+
}
167+
168+
handleUpdateWithProps(props) {
167169
if (props.onUpdate && typeof props.onUpdate === 'function') {
168170
props.onUpdate(this.el);
169171
}

webpack.config.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const webpack = require('webpack');
2+
3+
module.exports = {
4+
entry: ['babel-polyfill', 'react-hot-loader/patch', './dev/index.js'],
5+
output: {
6+
filename: 'bundle.js',
7+
},
8+
module: {
9+
rules: [
10+
{
11+
test: /\.js?$/,
12+
use: {
13+
loader: 'babel-loader',
14+
options: {
15+
presets: ['react', 'es2015'],
16+
plugins: [
17+
'react-hot-loader/babel',
18+
'transform-object-rest-spread',
19+
],
20+
},
21+
},
22+
exclude: /node_modules/,
23+
},
24+
],
25+
},
26+
devServer: {
27+
open: true,
28+
contentBase: './dev',
29+
},
30+
devtool: 'eval-source-map',
31+
};

0 commit comments

Comments
 (0)