如果我使用异步函数,则在reactjs中也不立即使用效果

发布于 2025-02-08 06:12:00 字数 1369 浏览 1 评论 0原文

基本上,每次我对数据库(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2025-02-15 06:12:00

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文