我正在尝试使用州获得TextField
的价值。我在其上添加了onChange
并致电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;
我收到以下错误
./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 |
任何人都可以帮我解决出错的问题吗?
答案 0 :(得分:2)
Method definition。由于您在类体声明中,因此需要替换
handleChange: function(event) {
this.setState({noteVal: event.target.value});
}
与
handleChange(event) {
this.setState({noteVal: event.target.value});
}