我有一个React组件,我想在单击时切换一个css类.
所以我有这个:
export class myComponent extends React.Component {
constructor() {
super();
this.state = { clicked: false };
this.handleClick = this.handleClick.bind(this);
}
render() {
return (
<div>
<div onClick={this.clicked}><span ref="btn" className="glyphicon"> </span></div>
</div>
);
}
handleClick() {
this.refs.btn.classList.toggle('active');
}
componentDidMount() {
this.refs.btn.addEventListener('click', this.handleClick);
this.setState({
clicked: this.state.clicked = true,
});
}
componentWillUnmount() {
this.refs.btn.removeEventListener('click', this.handleClick);
this.setState({
clicked: this.state.clicked = false,
});
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题是ESLint不断告诉我"this.refs"已经折旧了.
我该怎么做?如何修复它以便不使用折旧代码?
我正在研究将当前的Angular 1项目迁移到Angular 4的方法.
选项包括ng-forward,ngUpgrade或rewrite.
我正在考虑重写它,但有一点扭曲.
有人试过这个或知道更好的方法吗?
我想知道是否有任何ES6方式从网址获取json或其他数据.
jQuery GET和Ajax调用很常见,但我不想在这个中使用jQuery.
典型的调用如下所示:
var root = 'http://jsonplaceholder.typicode.com';
$.ajax({
url: root + '/posts/1',
method: 'GET'
}).then(function(data) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
或者没有像这样的jQuery:
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
else if (xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", "ajax_info.txt", true);
xmlhttp.send();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是......有没有新方法可以做到这一点......例如ES6还是它仍然是一样的?