3
3
33remido2021-09-03 15:11:50
React
33remido, 2021-09-03 15:11:50

Why doesn't setState change in if?



how to make it so that it does not return empty but full?

const customHook = () => {
let [state, setState] = useState('пустой');
  let arr = [1, 2, 3 ];
  if(arr.length > 1) {
    setState('полный');
    console.log(state) /* пустой */
  }

  if(arr.length < 1) {
    setState('пустой')
  }
  return {arr} /* пустой */
}

export default customHook;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Everybody_Lies, 2021-09-03
@Everybody_Lies

first link in google
try this

const customHook = () => {
let [state, setState] = useState('пустой');
  let arr = [1, 2, 3 ];
  if(arr.length > 1) {
    setState('полный', () => {
       console.log(state)
    });
  }

  if(arr.length < 1) {
    setState('пустой')
  }
  return {arr} /* пустой */
}

export default customHook;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question