конкретнее? сами просили )
import React, { Component } from 'react';
class Json extends Component {
constructor(props) {
super(props);
this.state = {
date: new Date(),
name: '',
profession: '',
age: 0
};
}
displayName(json){
try {
this.setState({name: json.name});
} catch (e) {
alert("Exception occured: " + e.message);
}
}
displayProfession(json){
try {
this.setState({profession: json.profession});
} catch (e) {
alert("Exception occured: " + e.message);
}
}
displayAge(json){
try {
this.setState({age: json.age});
} catch (e) {
console.log("Exception occured: " + e.message);
}
}
displayData(data){
try {
console.log(data);
let json = JSON.parse(data);
this.displayName(json);
this.displayProfession(json);
this.displayAge(json);
} catch (e) {
console.log("Exception occured: " + e.message);
}
}
getData() {
let request = new XMLHttpRequest();
let url = 'http://zf.loc';
request.open('GET', url);
request.addEventListener('load',
(function() {
if (request.status === 200) {
this.setState(JSON.parse(request.responseText));
} else {
console.log("Server Error: " + request.status);
}
}).bind(this) /*Вот здесь я пытаюсь вызвать displayData или setState и даже с .bind(this), но получаю "Type error: setState is not a function" или "displayName is not a function"*/
,false);
request.addEventListener('error', function () {
console.log("Can't Make AJAX Request");
}, false);
request.send();
}
componentDidMount() {
this.timerID = setInterval(
this.getData,
3000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
render() {
return (
<div>
<h1>Hello, {this.state.name}</h1>
<h2>Your profession is {this.state.profession}.</h2>
<h2> Your age {this.state.age}</h2>
</div>
);
}
}
export default Json;
"Балуюсь" с React и вот не видит у меня функция другую... Может не в контексте дело?