简体   繁体   English

为什么我不能使用挂钩在状态中设置值?

[英]Why can't I set Values in my state using hooks?

I'm trying to set data in my state over a function... the state is called as values and this is the code that I have 我正在尝试通过函数在我的状态下设置数据...状态被称为值,这是我拥有的代码

 const [values, setValues] = useState({
     about:'',ourServiceBuyer:'',ourServiceSupplier:'',faq:[],products:[]
 });

 useEffect(() => {

   async function handleData() {
     try {
       const result = await LandingData()

       console.log('algo',result.data.landing.about_us )

       setValues({  
         about:result.data.landing.about_us,
         ourServiceBuyer:result.data.landing.our_services_supplier,
         ourServiceSupplier :result.data.landing.our_services_supplier,
         faq : result.data.landing_page_faq,
         products:result.data.products, 
       });

       console.log("resultado de useEffect mostrando los values",values)
       console.log("resultado de useEffect", result);

     } catch (e) {

       console.error('error in obatin the return of LandingData: ', e.message);

       handleRedirect('/WrongRequest');
     }
   }
   handleData()

 }, []);

landingData() provides me with the data that I need, and I can see it with a console.log() But, when I try to set the data to the state I don't get anything only the same initial state... landingData()为我提供了所需的数据,我可以通过console.log()看到它。但是,当我尝试将数据设置为状态时,我得到的只是相同的初始状态……

i put "await" before this sentence 我在这句话前加上“等待”

console.log("resultado de useEffect mostrando los values",values) console.log(“ resultado de useEffect mostrando los values”,values)

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

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