2017-06-13 70 views
0

我正在尝试使用状态获得值TextField。我已在其上添加onChange并致电handleChange语法错误:React JS中handleChange上的意外标记(45:16)

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 | 

谁能帮助我什么地方错了?

+1

remove函数关键字和结肠更换

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

,写这样的:'handleChange(事件){' –

回答

2

Method definition。既然你是类主体声明中你需要

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

你的意思是,如果我定义一个班级以外的方法,那么我需要通过我的方法。这是 ? –

+1

这是什么意思“在课堂外定义方法”?在对象文字中,你可以使用两种语法'const obj = {fn(){return'es2015'},fnES5Way:function(){return'es5'}}'。如果你在类体声明中定义原型方法 - 只有短的方法。 –

+0

你是什么意思的“对象文字” –

相关问题