H
H
Harut Mkhitaryan2022-03-22 10:13:27
React
Harut Mkhitaryan, 2022-03-22 10:13:27

React.js need help just started learning but can't figure it out?

help me, I have such a problem, I just finished javascript, and have already begun to study react.js, you need to write the code so that when you click on the button from the bottom x to y, the number increases by one, and then reaching the number y decreases again, I wrote that would every time you press from to 100, each time you press it, the number would increase by one, but I don’t understand how to implement it back? Only decreases by 1 and that’s it?

import { useState } from "react";

function Button() {
const [count, setCount] = useState(0);
let welcome = () => setCountsetCount(count < 100 ? (count + 1) : (count - 1));
return (

{count}
Click

);
}

export default Button;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Derbenchenko, 2022-03-22
@harut997

I think we need a flag, let's say direct and let's say it initially has the value asc.
in the handler function, there is a check for direction, if it is in the asc value, count is increased by 1, if not, a decrease
inside each branch is checked with count. in the asc branch, if count >= 100 && setDirection(desc), in the desc branch - count <= 100 && setDirection(asc)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question