Javascript 使用状态';s setState在函数handleNew中未设置值,我必须

Javascript 使用状态';s setState在函数handleNew中未设置值,我必须,javascript,function,api,async-await,compiler-errors,Javascript,Function,Api,Async Await,Compiler Errors,SearchBar组件(包含从用户获取输入的输入类型文本)从用户返回搜索查询,我可以将其传递到app.js,但我必须执行两次事件,即两次按enter键以生成API以获取数据,但我甚至无法设置“video”的值。 从“React”导入React; 从“@material ui/core”导入{Grid}”; 导入“/App.css”; 从“react”导入{useState,useffect}; 从“/api/youtube”导入youtube; //从“/components/SearchBar

SearchBar组件(包含从用户获取输入的输入类型文本)从用户返回搜索查询,我可以将其传递到app.js,但我必须执行两次事件,即两次按enter键以生成API以获取数据,但我甚至无法设置“video”的值。

从“React”导入React;
从“@material ui/core”导入{Grid}”;
导入“/App.css”;
从“react”导入{useState,useffect};
从“/api/youtube”导入youtube;
//从“/components/SearchBar”导入搜索栏;
//从“/components/VideoDetail”导入VideoDetail;
从“/components/index”导入{SearchBar,VideoDetail,Video}”;
从“/components/VideoList”导入视频列表;
常量应用=()=>{
const[video,setVideo]=useState([]);
const[selectedVideo,setselectedVideo]=使用状态(null);
“值”是来自用户的输入

  const handleNew = async (value) => {
    console.log("hello", value);

    const {
      data: { items: videoz },
    } = await youtube.get("search", {
      params: {
        part: "snippet",
        maxResults: 6,
        key: [API_KEY],
        q: value,
      },
    });
    const lists = [videoz[0]];
    console.log(videoz);
    setselectedVideo(videoz[0]);
    console.log(selectedVideo);
  };

  return (
    <div class="container">
      <nav>
        <SearchBar onChange={handleChange} />
      </nav>
      <main>
        <VideoDetail video={selectedVideo} />
      </main>
      <div id="sidebar">Sidebar</div>
    </div>
    // <div className="wrapper">
    //   <SearchBar
    //     className="search-bar"
    //     // onFormSubmit={handleNew}
    //     // value={query}
    //     onChange={handleChange}
    //   />
    //   <div className="wrapped">
    //     <VideoDetail className="videozzz" video={selectedVideo} />
    //     <VideoList className="" />
    //   </div>
    // </div>
  );
};

export default App;
// function random_bg_color() {
//   var x = Math.floor(Math.random() * 256);
//   var y = Math.floor(Math.random() * 256);
//   var z = Math.floor(Math.random() * 256);
//   var bgColor = "rgb(" + x + "," + y + "," + z + ")";
//   console.log(bgColor);
//   document.body.style.backgroundImage =
//     "url("https://image.tmdb.org/t/p/original//86L8wqGMDbwURPni2t7FQ0nDjsH.jpg")";
//   document.body.style.background.url = bgColor;
// }

// random_bg_color();

const handleNew=async(值)=>{
log(“hello”,value);
常数{
数据:{items:videoz},
}=等待youtube.get(“搜索”{
参数:{
部分:“片段”,
最大结果:6,
密钥:[API_密钥],
问:价值观,
},
});
常量列表=[videoz[0]];
console.log(videoz);
设置选定视频(videoz[0]);
console.log(selectedVideo);
};
返回(
边栏
// 
//   
//   
//     
//     
//   
// 
);
};
导出默认应用程序;
//函数random_bg_color(){
//var x=Math.floor(Math.random()*256);
//var y=Math.floor(Math.random()*256);
//var z=Math.floor(Math.random()*256);
//var bgColor=“rgb(“+x+”、“+y+”、“+z+”);
//console.log(bgColor);
//document.body.style.backgroundImage=
//“url(”https://image.tmdb.org/t/p/original//86L8wqGMDbwURPni2t7FQ0nDjsH.jpg")";
//document.body.style.background.url=bgColor;
// }
//随机_bg_color();