bind your function or use arrow function
componentWillMount = () => {
axios("https://jsonplaceholder.typicode.com/Posts")
.then((response) => {
this.setState({ Posts: response.data[0].title });
})
.catch(function (error) {
console.log(error);
});
}
Edit
please use componentDidMount
instead of componentWillMount
componentDidMount = () => {}
or save the value of this in a variable.
componentDidMount = () => {
const that = this;
axios("https://jsonplaceholder.typicode.com/Posts")
.then(function (response) {
that.setState({ Posts: response.data[0].title });
})
.catch(function (error) {
console.log(error);
});
}
DEMO
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id="root"></div>
<script type="text/babel">
class App extends React.Component {
constructor() {
super();
this.state = {
name: 'React',
Posts: '',
};
}
componentDidMount = () => {
const that = this;
axios("https://jsonplaceholder.typicode.com/Posts")
.then(function (response) {
that.setState({ Posts: response.data[0].title });
})
.catch(function (error) {
console.log(error);
});
}
render() {
return (
<div>
<p>Post title</p>
<p>{this.state.Posts}</p>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
</script>
this
This is determined at runtime and depending on the code, it can be something different.
this is
- determined at runtime, when a function is envoked
- determined by how a function is invoked, not where the function is
defined
- a reference to an object.
- will always be an object
- global (this) not available in strict mode
Example 1: this = window
var name = 'Global';
var callName1 = function() {
var name = 'Peter';
console.log('--- From callName1 ----');
console.log(this.name);
//console.log(this);
callName2();
}
var callName2 = function() {
var name = 'Jane';
console.log('--- From callName2 ----');
console.log(this.name);
//console.log(this);
}
callName1();
var execute = function(fn) {
var name = 'Mary';
console.log('--- From execute ----');
console.log(this.name);
//console.log(this);
}
execute(callName2);
Example 2: not available in strict mode
'use strict';
var name = 'Global';
var callName1 = function() {
var name = 'Peter';
console.log('--- From callName1 ----');
console.log(this.name);
console.log(this);
}
callName1();
Example 3: examining this with method invocation
var name = 'global';
var obj = {
name: 'James Obj1',
func: function() {
console.log('--- From func ----');
console.log(this.name);
console.log(this); // this reference obj1
}
}
obj.func()
var obj2 = {
name: 'Jame Obj2',
func: obj.func // this reference obj2, but the function is defined in obj1
}
obj2.func()
var obj3 = {
name: 'Kane Obj3',
obj4: {
name: 'Mary Obj4',
func: function () {
console.log('--- From obj4 ----');
console.log(this.name);
console.log(this); // this reference obj4
}
}
}
obj3.obj4.func()
With () => {}
function this - is lexically bound. It means that it uses this
from the code that contains the arrow function.