admin管理员组文章数量:1410689
I am trying to implement a simple react component which change name on clicking. I am getting the error Error - TypeError: this is undefined
while loading my page which has below react component. The error seems to be in the setState
line.
class Layer5 extends React.Component {
constructor(props) {
super(props);
this.state = {buttonstate: false};
}
componentDidMount() {
}
componentWillUnmount() {
}
handleClick(e) {
e.preventDefault();
this.setState({buttonstate: !this.state.buttonstate});
}
render() {
var icon = this.state.buttonstate ? 'images/image1.png' : 'images/image2.jpg';
return (
<div className="layer5">
<a href="#" onClick={this.handleClick}><img src={icon} ></img></a>
</div>
);
}
}
I am trying to implement a simple react component which change name on clicking. I am getting the error Error - TypeError: this is undefined
while loading my page which has below react component. The error seems to be in the setState
line.
class Layer5 extends React.Component {
constructor(props) {
super(props);
this.state = {buttonstate: false};
}
componentDidMount() {
}
componentWillUnmount() {
}
handleClick(e) {
e.preventDefault();
this.setState({buttonstate: !this.state.buttonstate});
}
render() {
var icon = this.state.buttonstate ? 'images/image1.png' : 'images/image2.jpg';
return (
<div className="layer5">
<a href="#" onClick={this.handleClick}><img src={icon} ></img></a>
</div>
);
}
}
Share
Improve this question
edited Oct 25, 2019 at 8:15
Kalle Richter
8,72829 gold badges91 silver badges203 bronze badges
asked Dec 4, 2017 at 3:21
codinglovercodinglover
1991 gold badge2 silver badges12 bronze badges
2
|
2 Answers
Reset to default 18Either a) create a property lexically closed over the instance (this
) using an arrow function or b) use .bind
.
Do one or the other.
either a)
class Layer5 extends React.Component {
constructor(props) {
super(props);
this.handleClick = e => {
e.preventDefault();
this.setState({buttonstate: !this.state.buttonstate});
};
}
}
or b)
render() {
const icon = this.state.buttonstate ? 'images/image1.png' : 'images/image2.jpg';
return <div className="layer5">
<a href="#" onClick={this.handleClick.bind(this)}>
<img src={icon} >
</img>
</a>
</div>;
}
}
Note that some prefer to cache the bound function created by .bind
but this is simply an optimization.
import React from 'react'
class UsernameForm extends React.Component {
constructor(props) {
super(props)
this.state = {
username: ''
}
this.onchange=this.onChange.bind(this)
this.onSubmit=this.onSubmit.bind(this)
}
onChange(e) {
this.setState({
username: e.target.value
})
}
onSubmit(e) {
e.preventDefault()
this.props.onSubmit(this.state.username)
}
render() {
return (
<div>
<form onSubmit={this.onSubmit}>
<input type='text'placeholder='What is your username ?' onChange={this.onChange} />
<input type='submit' />
</form>
</div>
)
}
}
export default UsernameForm
I am getting error: this is undefined
So that i bind this on {this.onChange} see the solution below
import React from 'react'
class UsernameForm extends React.Component {
constructor(props) {
super(props)
this.state = {
username: ''
}
this.onchange=this.onChange.bind(this)
this.onSubmit=this.onSubmit.bind(this)
}
onChange(e) {
this.setState({
username: e.target.value
})
}
onSubmit(e) {
e.preventDefault()
this.props.onSubmit(this.state.username)
}
render() {
return (
<div>
<form onSubmit={this.onSubmit}>
<input type='text'placeholder='What is your username ?' onChange={this.onChange.bind(this)} />
<input type='submit' />
</form>
</div>
)
}
}
export default UsernameForm
本文标签: htmlTypeError this is undefinedReact JSJavaScriptStack Overflow
版权声明:本文标题:html - TypeError: this is undefined - React JS, Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738604381a2102260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
handleClick
... common solution is to have the linethis.handleClick = this.handleClick.bind(this)
inside the constructor - becausethis
is tricky with event handlers – Jaromanda X Commented Dec 4, 2017 at 3:24