S
S
sema-fedotov2021-09-09 21:50:07
React
sema-fedotov, 2021-09-09 21:50:07

How to pass a new state to a function in react?

Good afternoon, there is an application written in React:

const App = () => {
     const [status, setStatus] = useState(null)

     function getStatus() {
         console.log(status)
     }

}


The application has many states. Is it possible to somehow track their change in a function without useEffect with binding to one state?
useEffect(() => {
       // ...
    }, [status])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2021-09-10
@slide13

There is essentially only one option, probably - to save the state in ref.
But you still have to update the ref in useEffect :

useEffect(() => {
    ref.current = status;
  }, [status]);

From ref already read the status in your getStatus function, where ref.current will always contain the latest status value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question