Skip to content

Commit 7019f11

Browse files
committed
Getting front end to run with fat arrow methods so that 'this' resolves.
(All file references are to `src/capstone/front`.) 1. `app.js` was defining methods using function syntax: onEnd (end){ this.setState({ end: end }) } but the browser would complain at runtime that `this` wasn't defined. 2. Changed to fat arrow syntax in order to get `this` defined as per <https://stackoverflow.com/questions/33973648/react-this-is-undefined-inside-a-component-function>: onEnd = (end) => { this.setState({ end: end }) } but Parcel complained about an unexpected token (it doesn't like fat arrow syntax). 3. Discovered that we need to tell Babel to translate fat arrow syntax <https://stackoverflow.com/questions/31362292/how-to-use-arrow-functions-public-class-fields-as-class-methods>, so added `.babelrc` with React defaults *and* the command to transform class properties: { "presets": [ "react" ], "plugins": [ "transform-class-properties" ] } Had to install a couple of packages to make this work: "babel-plugin-transform-class-properties": "^6.24.1", "babel-preset-react": "^6.24.1", 4. Commented out the calls to Chart.js stuff so that Parcel wouldn't "helpfully" try to install it along the way.
1 parent 4f761f0 commit 7019f11

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
"react"
4+
],
5+
"plugins": [
6+
"transform-class-properties"
7+
]
8+
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
]
3232
},
3333
"dependencies": {
34+
"babel-plugin-transform-class-properties": "^6.24.1",
35+
"babel-preset-react": "^6.24.1",
3436
"cors": "^2.8.4",
3537
"express": "^4.16.3",
3638
"express-winston": "^3.0.0",

src/capstone/front/DataChart.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import React from 'react'
2+
/*
23
import {Scatter} from 'react-chartjs-2';
4+
*/
35

46
const DataChart = ({data}) => {
7+
return <p>NO CHART</p>
58

9+
/*
610
if (! data) {
711
return (<p>no data</p>)
812
}
@@ -30,6 +34,7 @@ const DataChart = ({data}) => {
3034
return (
3135
<Scatter data={data} options={options}/>
3236
)
37+
*/
3338
}
3439

3540
export default DataChart

src/capstone/front/app.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class App extends React.Component {
1818
}
1919
}
2020

21-
componentDidMount() {
21+
componentDidMount = () => {
2222
const url = `${this.baseUrl}/survey/stats`
2323
fetch(url).then((response) => {
2424
return response.json()
@@ -29,19 +29,19 @@ class App extends React.Component {
2929
})
3030
}
3131

32-
onStart(start) {
32+
onStart = (start) => {
3333
this.setState({
3434
start: start
3535
})
3636
}
3737

38-
onEnd(end) {
38+
onEnd = (end) => {
3939
this.setState({
4040
end: end
4141
})
4242
}
4343

44-
onNewRange() {
44+
onNewRange = () => {
4545
const params = {
4646
method: 'GET',
4747
headers: {
@@ -59,7 +59,7 @@ class App extends React.Component {
5959
})
6060
}
6161

62-
render() {
62+
render = () => {
6363
const tableStyle = {overflow: 'scroll', height: '200px'}
6464
return (
6565
<div>
@@ -80,5 +80,5 @@ class App extends React.Component {
8080

8181
ReactDOM.render(
8282
<App />,
83-
document.getElementById("app")
83+
document.getElementById('app')
8484
)

0 commit comments

Comments
 (0)