Williams Williams -3 years ago 173
React JSX Question

Syntax error: Unexpected token (45:16) on handleChange in React JS

I am trying to get value of

TextField
using state. I have added
onChange
on it and calling
handleChange
.

class App extends Component {
constructor(props) {
injectTapEventPlugin();
super(props);
this.state = { open: false, noteVal: "" };
console.log(this.state);
}

handleOpen = () => {
this.setState({ open: true });
console.log(this.state);
};

handleClose = () => {
this.setState({ open: false });
};

handleCreateNote = () => {
console.log("Hey");
};

handleChange: function(event) {
this.setState({noteVal: event.target.value});
}

render() {
return (
<MuiThemeProvider>
<div>
<AppBarTest />
<FloatingActionButton
style={styles.fab}
backgroundColor={colors.blue_color}
onTouchTap={this.handleOpen}
>
<ContentAdd />
</FloatingActionButton>
<Dialog
open={this.state.open}
onRequestClose={this.handleClose}
title={strings.dialog_create_note_title}
>
<TextField
hintText="Note"
style={{ width: "48%", float: "left", height: 48 }}
defaultValue={this.state.noteVal}
onChange={this.handleChange}
/>

<div
style={{
width: "4%",
height: "48",
backgroundColor: "red",
float: "left",
visibility: "hidden"
}}
/>

<FlatButton
label={strings.create_note}
style={{ width: "48%", height: 48, float: "left" }}
onTouchTap={this.handleCreateNote}
/>
</Dialog>
</div>
</MuiThemeProvider>
);
}
}

export default App;


I am getting below error

./src/App.js
Syntax error: D:/React JS/todo-app/src/App.js: Unexpected token (45:16)

43 | };
44 |
> 45 | handleChange: function(event) {
| ^
46 | this.setState({noteVal: event.target.value});
47 | }
48 |


Can anyone help me what has went wrong?

Answer Source

Method definition. Since you are inside class body declaration you need to replace

 handleChange: function(event) {
    this.setState({noteVal: event.target.value});
  }

with

 handleChange(event) {
    this.setState({noteVal: event.target.value});
  }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download