Need help why am I getting undefined here how do I fix it?

Viewed 29

I'm getting undefined here. How can I load the data in useEffect and when everything is loaded how do I store it in useState variable and render things i need ?

const appDesign = require(`./data/${subcategory}`)

  useEffect(() => {if (router.asPath !== router.route) { }  }, [router])
1 Answers

I had not used the next.js before

But if you want to load something data by useEffect(), and store state in your component
declare a state var first

const [state, setState] = useState(0);

Then you should load the data and set state in useEffect's callback function
example:

useEffect(()=>{
  if (router) {
   // load some data or request api 
   // And set state
   let a = 1;
   setState(a);
  }
}, [router]);

more info please refer Using the State Hook

Related