Beginnerprogrammer Beginnerprogrammer - 1 year ago 121
React JSX Question

ReactJs print value of input field

Iam trying to print instantly my input value in the render function. But now I am looking at this link:

Here they use an constructor with super(props) and than set the state.

but when I try this:

class App extends React.Component {
constructor(props) {

this.state = {text: ''};

handleChange(event) {
this.setState({ text: });

render() {
return (
<h1>Hello, world!</h1>
<h2>It is {this.state.text}.</h2>

<input type="text" onKeyUp={this.handleChange} />

It throws me this error:
Uncaught TypeError: Cannot read property 'state' of undefined

How do I solve this.

Answer Source

when you call a function like that, it is called by the window, not by your react object.

To make the function be bound to your react object (and have the ability to use the setState method, you need to use this:


this will bind it to your react object :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download