Popular use cases
How to use state with hooks in React
Last updated Jan 23, 2020
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<>
<h1>{count} times</h1>
<button onClick={handleClick}>Add 1</button>
</>
);
}
How to interpolate in JavaScript
Last updated Nov 11, 2017
const name = "John";
console.log(`Welcome ${name}.
You have ${2 * 5} new notifications!`);
Welcome John.
You have 10 new notifications!
How to listen to click event in JavaScript
Last updated Nov 11, 2017
<div id="box"></div>
const element = document.querySelector("#box");
element.addEventListener("click", event => {
console.log("Element clicked");
});