-2

This is my code:

const [tab, setTab] = useState("a")

const setTabCustom = (value: string) => {
    console.log(value)
    setTab(value)
    anotherFunc();
}

const anotherFunc = () => {
    console.log(tab)
}

<span onClick={() => setTabCustom("b")}>Click</span>

And this is result:

b
a

Why? I use setTab and after anotherFunc most show this result:

b
b

Live code: https://playcode.io/900225

  • First, you're outputting the argument to change the state and then in your other function you log the value of actual state (tab). It takes a while to change the state, but if you were to output the `tab` value in your JSX, you would see the correct result. (A - initial value, then B - manualy set after) – kasperoo May 19 '22 at 09:27

0 Answers0