Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from "react";
- import "./style.css";
- export default function App() {
- const [cnt, setCnt] = React.useState(function() {
- return Math.floor(Math.random(1) * 10);
- });
- const [k, setK] = React.useState(() => 20);
- React.useEffect(
- function() {
- console.log("rendered");
- },
- [k]//optional
- );
- function increment() {
- setCnt(function(prevCnt) {
- return prevCnt + 1;
- });
- }
- function decrement() {
- setCnt(function(prevCnt) {
- return prevCnt - 1;
- });
- }
- function modK() {
- setK(pk => pk + 1);
- }
- return (
- <React.Fragment>
- <button onClick={decrement}>-</button>
- <b>{cnt}</b>
- <button onClick={increment}>+</button>
- <br />
- <b>{k}</b>
- <button onClick={modK}>mod k</button>
- </React.Fragment>
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement