usestate access previous state
import React, { useState } from "react";
import ReactDOM from "react-dom";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h1>{count}</h1>
<button onClick={() => setTimeout(() => setCount(count + 1), 2000)}>
Delayed Counter (basic)
</button>
<button onClick={() => setTimeout(() => setCount(x => x + 1), 2000)}>
Delayed Counter (functional)
</button>
<button onClick={() => setCount(count + 1)}>Immediate Counter</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Counter />, rootElement);
const [arrayOfObjs, handleObjSelection] = useState([]);
// on a buttton for example
<button
onClick={selectedObj => handleObjSelection(
prevSelected => [...prevSelected, selectedObj],
))}
>
Also in JavaScript:
- how to update jquery datatable row cell value
- reactstrap search bar
- how to delete node_modules
- react native curved view
- js email regex
- javascript test regex date with slashes
- how to sent react from data in mongodb
- detect if two line segments intersect each other javascript
- function in javascript
- reach to each cell in 2d array javascript
- javascript string concatenation
- js scroll to bottom of element
- array javascript
- removing first item array js
- javascript get values from object
- javascript class in external file
- how to disable right click in javascript
- create element javascript with class
- remove header border react native
- bcrypt_lib.node not found
- if isset handlebars js
- javascript 2 decimal float array elements
- fire off some javascript on page load
- vue axios catch error