简体   繁体   English

需要帮助为什么我在这里变得未定义我该如何解决?

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

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?如何在useEffect中加载数据以及加载所有内容时如何将其存储在useState变量中并呈现我需要的东西?

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

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

I had not used the next.js before我之前没有使用过next.js

But if you want to load something data by useEffect() , and store state in your component但是,如果您想通过useEffect()加载某些数据,并将 state 存储在您的组件中
declare a state var first首先声明一个 state var

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

Then you should load the data and set state in useEffect's callback function然后你应该加载数据并在useEffect的回调function中设置state
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更多信息请参阅使用 State 挂钩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM