当 useState 更改不起作用时,在 useEffect 中进行清理

时间:2021-04-17 20:12:22

标签: react-native react-hooks use-effect

我使用 fetch 来获取数据如下:

const [regstatus, setRegstatus] = useState("");
     useEffect(() => {
            
            fetch('url*********************')
    
                .then((response) => response.json())
                .then((res) => {
    
                    if (res.success === true) {
    
                        setRegstatus(res.status);
    
                    }
    
                })

 }, [regstatus]);

所以我使用依赖项 [regstatus] 在它改变后进行清理,但这对我不起作用,因为在屏幕上我正在打印这个 regstatus,如下所示:

    {regstatus == "pending" ?

                    <View>
                     /* show something here */

                    </View>

                    : 
                        <View>
                     /* show something here */

                    </View>

                    }

因此,这里一旦 regstatus 更改,它不会在上述三元条件下执行相关视图,因为我需要关闭应用程序并再次打开以显示更新

0 个答案:

没有答案