如果我使用异步函数,则在reactjs中也不立即使用效果
基本上,每次我对数据库(sqlite3)进行操作时,它不会立即在页面上呈现,但是我必须单击一些其他组件才能进行更新,否则我必须刷新页面/重置服务器服务器,
这是我很snippet我很snippet在每个页面组件中使用:
const fetchData= async()=>{
let updatedVector= await API.getUpdate();
for (let i = 0;i<props.courses.length;i++){
for (let j=0;j<updatedVector.length;j++){
if(props.courses[i].code===updatedVector[j].code){
props.courses[i].current= await API.getSpecificData(props.courses[i].code);
}
}
}
}
//
useEffect(()=>{
fetchData();
},[fetchData])
它确实在数据库上进行更新,但是需要一些时间才能更新组件,是否有任何方法可以立即更新?或者我必须刷新应用程序。
编辑:我也通过了SetState,但是我必须在组件链接上至少单击两次,然后我进行了更新:
//get updates
useEffect(()=>{
console.log("triggered")
const fetchData= async()=>{
let updatedVector= await API.getUpdate();
for (let i = 0;i<props.courses.length;i++){
for (let j=0;j<updatedVector.length;j++){
if(props.courses[i].code===updatedVector[j].code){
props.courses[i].current= await API.getSpecificData(props.courses[i].code);
}
}
}
props.setCourses(props.courses)
}
fetchData();
},[fetchData])
Basically every time I do a operation to my database (sqlite3) it does not render immediately on the page, but I have to click on some other component to update or I have to refresh the page/reset the server
This is the snippet I quite use in every page component:
const fetchData= async()=>{
let updatedVector= await API.getUpdate();
for (let i = 0;i<props.courses.length;i++){
for (let j=0;j<updatedVector.length;j++){
if(props.courses[i].code===updatedVector[j].code){
props.courses[i].current= await API.getSpecificData(props.courses[i].code);
}
}
}
}
//
useEffect(()=>{
fetchData();
},[fetchData])
It does update on the database but takes some time to update on the component, is there any way to update like in an immediate way? Or I have to refresh the application.
EDIT: I passed the setState too, but yet I have to click at least twice on the component link and then I have my update:
//get updates
useEffect(()=>{
console.log("triggered")
const fetchData= async()=>{
let updatedVector= await API.getUpdate();
for (let i = 0;i<props.courses.length;i++){
for (let j=0;j<updatedVector.length;j++){
if(props.courses[i].code===updatedVector[j].code){
props.courses[i].current= await API.getSpecificData(props.courses[i].code);
}
}
}
props.setCourses(props.courses)
}
fetchData();
},[fetchData])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
props.courses [i] .current useref?
如果是这样,请尝试使用Usestate来处理您的数据,您将不会有重新渲染问题。
Is props.courses[i].current a useRef?
if so try using useState to handle your data, u won't have a re-render problem.