0

I'm trying to set array using useState feature

function SelectCategoryList() {
        const [presentDataArray, setPresentDataArray] = useState([]);
        const hello = ['hello', 'bye'];

        useEffect(() => {
            console.log(hello);
            setPresentDataArray([...hello]);
            console.log(presentDataArray);
        }, []);
}

first console : ['hello', 'bye']
second console : []

Why presentDataArray does not set value by helloArray

RayKim
  • 423
  • 1
  • 6
  • 22
  • https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately refer this ,you will get you answer of console – Kishan Jaiswal Sep 20 '19 at 05:32
  • same like setState useState is not synchronous , so after setting the value you cant view the value immediately . Move the console to the return method and check – pageNotfoUnd Sep 20 '19 at 05:32
  • @pageNotfoUnd What do you mean that 'Move the console to the return method and check'? – RayKim Sep 20 '19 at 05:39
  • How to log the data only once which is updated in useEffect. @KishanJaiswal – RayKim Sep 20 '19 at 05:42
  • 1
    @RayKim place the console inside the return function of your component to check the updated state value – pageNotfoUnd Sep 20 '19 at 05:44
  • @pageNotfoUnd It works!! Thank you very much. I didn't understand at all about this and manage to figure out how to log this value in my head. Thank you. – RayKim Sep 20 '19 at 05:48

0 Answers0