2
我试图捕捉的onChange事件的输入,并呼吁的setState新值,但此后只要我在输入型我得到:React.js - “本”甚至不确定结合
Uncaught TypeError: Cannot read property 'setState' of undefined
即使我已经叫
this.handleChange.bind(this)
在构造
index.js
import React from 'react'
import * as ReactDOM from "react-dom";
import App from './App'
ReactDOM.render(
<App />,
document.getElementById('root')
);
App.js
import * as React from "react";
export default class App extends React.Component {
constructor(props) {
super(props)
this.handleChange.bind(this)
this.state = {contents: 'initialContent'}
}
handleChange(event) {
this.setState({contents: event.target.value})
}
render() {
return (
<div>
Contents = {this.state.contents}
<input type="text" onChange={this.handleChange}/>
</div>
);
}
}