fetch('https://example.com/api')
.then(response => response.json())
.then(json => dispatch(someAction(json)))
.catch(console.log);
const handleFiles = () => {
return async handle => {
let result;
result = 'hello';
return result;
};
};
handleFiles()().then(result => console.log(result)).catch(console.log(e));
const handleFiles = async () => {
let result;
result = 'hello';
return result;
};
handleFiles().then(result => console.log(result)).catch(console.log(e));
class App extends React.Component {
state = {
category: 'business',
items: [],
};
componentDidMount() {
this.fetchData();
}
fetchData() {
axios.get('https://newsapi.org/v2/top-headlines?country=us&category='+this.state.category)
.then(res => {
this.setState({ items: res.data.articles });
});
}
changeCategory = () => {
this.setState(
{ category: 'entertainment' },
this.fetchData,
);
};
render() {
return (
<div className='wrapper'>
<button onClick={this.changeCategory}>CLICK</button>
</div>
);
}
}
<input class="js-search" />
<a class="js-search-link" target="_blank" href="#">Search in Google</a>
var defaultHref = 'https://google.com/search?q=';
var search = document.querySelector('.js-search');
var link = document.querySelector('.js-search-link');
link.setAttribute('href', defaultHref);
search.addEventListener('input', function() {
link.setAttribute('href', defaultHref + search.value.trim().replace(/ +/, '+'));
});
function startTimer(secs) {
var i = 0;
var timer = document.getElementById('timer');
var interval = setInterval(function() {
if (++i >= secs) {
clearInterval(interval);
timer.innerHTML = 'Done!';
} else {
++timer.innerHTML;
}
}, 1000);
}
startTimer(10);
Демо.function startTimer(secs) {
var timer = document.getElementById('timer');
setTimeout(function() {
timer.innerHTML = 'Done!';
}, secs * 1000);
}
startTimer(10);
Демо. function debounce(f, ms) {
let timer = null;
return function (...args) {
const onComplete = () => {
f.apply(this, args);
timer = null;
}
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(onComplete, ms);
};
}
function throttle(func, ms) {
var isThrottled = false,
savedArgs,
savedThis;
function wrapper() {
if (isThrottled) { // (2)
savedArgs = arguments;
savedThis = this;
return;
}
func.apply(this, arguments); // (1)
isThrottled = true;
setTimeout(function() {
isThrottled = false; // (3)
if (savedArgs) {
wrapper.apply(savedThis, savedArgs);
savedArgs = savedThis = null;
}
}, ms);
}
return wrapper;
}
function handler() {
// do something
}
var debouncedHandler = debounce(handler, 500);
document.querySelecror('.submit').addEventListener('click', debouncedHandler);
{
counterReducer: 0,
}
function mapStateToProps(state) {
return {
count: state.counterReducer,
};
}
const mapStateToProps = state => ({
count: state.counterReducer,
});
Есть родительский класс и есть дочерний класс.