[英]How to update component information in react native?
I'm consuming the data from this api, using useEffect.我正在使用 useEffect 使用此 api 中的数据。 What I want, is that every time the data changes in the API.
我想要的是,每次 API 中的数据发生变化。 Change component information
更改组件信息
const [infoUser, setInfoUser] = useState([]);
const getInfoUser = async () => {
try {
const accessToken = await AsyncStorage.getItem('@access_token');
axios
.get(
'https://apiexample.com/cliete',
{
headers: { Authorization: `Bearer ${accessToken}` },
},
)
.then(function (response) {
// handle success
console.log('DADOS USER:', response.data.data);
const userData = response.data.data;
setInfoUser([...infoUser, userData]);
})
.catch(function (error) {
// handle error
console.log(error);
});
} catch (e) {
// error reading value
console.log('Erro de token', e);
}
};
useEffect(() => {
getInfoUser();
}, []);
On another screen, I change the data and save.在另一个屏幕上,我更改数据并保存。
But the previous screen has not been updated.但是之前的画面并没有更新。 The data only changes when I log out and enter the app again
仅当我注销并再次进入应用程序时数据才会更改
{infoUser.map(user => (
<Text style={styles.subtitle}>Nome Completo</Text>
<Text style={styles.title}>{user.nomeCompleto}</Text>
</TouchableOpacity>
))}
If you pass variables/functions to useEffect
array of dependencies, and they are changing, it will trigger your useEffect
.如果您将变量/函数传递给
useEffect
依赖数组,并且它们正在发生变化,它将触发您的useEffect
。 So if you want to depend on some data
you can write it like this:所以如果你想依赖一些
data
,你可以这样写:
useEffect(() => {
getInfoUser();
}, [data]);
Every time data
is updated useEffect
will be called by React.每次更新
data
时,React 都会调用useEffect
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.