I have a problem with react components and props. Here's exception below. What I'm doing wrong?
Uncaught TypeError: Super expression must either be null or a function, not undefined
class Component extends React.component {
constructor(props) {
super(props);
this.state = {
name: ''
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
name: event.target.value
});
}
render() {
return (
<div>
<input type="text" onChange={this.handleChange} />
<p>{this.state.name}</p>
</div>
)
}
};
ReactDOM.render(
<Component />,
document.getElementById('reactContainer')
)
Codepen link
Issue is in this line:
class Component extends React.component {
you used small c in React.component instead of that use C:
class Component extends React.Component {
Check the working code:
class Component extends React.Component {
constructor(props) {
super(props);
this.state = {
name: ''
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({
name: event.target.value
});
}
render() {
return (
<div>
<input type="text" onChange={this.handleChange} />
<p>{this.state.name}</p>
</div>
)
}
};
ReactDOM.render(
<Component />,
document.getElementById('reactContainer')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='reactContainer'></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With