TypeError: cannot read property setState of undefined
Date : March 29 2020, 07:55 AM
hope this fix your issue Its a context issue, your mistake is that you didn't bind all the way down to the anonymous function. What you probably want to do is use arrow functions, try this: componentDidMount() {
axios.get(`http://192.168.1.9:8082`)
.then(data => this.setState({dos:data}););
}
componentDidMount() {
axios.get(`http://192.168.1.9:8082`)
.then(function(data) {
console.log(data);
this.setState({dos:data});
}).bind(this);
}
|
TypeError: Cannot read property 'setState' of undefined
Date : March 29 2020, 07:55 AM
like below fixes the issue Bind the callback function also so that this inside the callback points to the context of the React Component and not the callback function getPosts = () => {
$.ajax({
type: 'get',
url: urlname,
success: (data) => {
this.setState( { posts: data } )
}
});
}
getPosts = () => {
$.ajax({
type: 'get',
url: urlname,
success: function(data) {
this.setState({ posts: data })
}.bind(this)
});
}
|
TypeError: Cannot read property 'setState' of undefined/XML
Date : March 29 2020, 07:55 AM
it should still fix some issue You're mixing and matching function()s that don't capture this and arrow (=>) functions which do. Simply use arrow functions everywhere and the this (that's your React component) will be properly captured: axios
.get(session_url)
.then(response => {
parseString(response.data, (err, result) => {
if (err) {
throw err;
} else {
this.setState({
odm: result.data,
loading: false,
});
}
});
})
.catch(error => {
console.log(error);
});
// promisified version of `parseString`:
const parseStringP = data =>
new Promise((resolve, reject) =>
parseString(response.data, (err, result) => {
if (err) return reject(err);
resolve(result);
}),
);
// ...
try {
const response = await axios.get(session_url);
const result = await parseStringP(response.data);
this.setState({
odm: result.data,
loading: false,
});
} catch (error) {
console.log(error);
}
|
Rejection (TypeError): Cannot read property 'setState' of undefined, Uncaught (in promise) TypeError: Cannot read proper
Date : March 29 2020, 07:55 AM
Does that help To solve the setState issue, you need to be aware that the async.auto({}) is a promise. The final error function need to be deleted this code: function (err, results)
{
this.setState({active:results.optionsFill})
console.log('error: ', err)
}
.then((result)=>{
this.setState({active: result.optionsFill})
})
|
react setstate TypeError: Cannot read property 'setState' of undefined
Tag : reactjs , By : Hitesh Prajapati
Date : March 29 2020, 07:55 AM
around this issue , Either bind this to your function in the constructor constructor(checked){
super(checked);
this.state={checked:false}
this.handleChange = this.handleChange.bind(this);
}
<input type="checkbox" onChange={(e)=>this.handleChange(e)} />
|