如何在 React Native 中更新函数内部的状态?
2021-04-01
86
我有一个简单的功能组件,根据状态显示“添加项目”或“删除项目”按钮:
const SimpleComponent = ({ route }) => {
const { id } = route.params;
const [add, setAdd] = useState(false);
useEffect(() => {
{ isCurrentUserHavingThisItem(route, id) ?
setAdd(true)
:
setAdd(false)
}
}, [])
return (
<View >
{ add ?
(
<TouchableOpacity onPress={() => removeItem(route, id)}>
<Text> Remove this item </Text>
</TouchableOpacity>
)
:(
<TouchableOpacity onPress={() => addItem(route, id)}>
<Text> Add item </Text>
</TouchableOpacity>
)
}
</View>
);
};
const isCurrentUserHavingThisItem = (route, id) => {
firebase.firestore()
.collection('Users')
.doc(id)
.collection('Items')
.doc(route.params.item.key)
.get()
.then((docSnapshot) => {
console.log("my return statement: ", docSnapshot.exists);
setAttending(docSnapshot.exists);
return docSnapshot.exists;
})
.catch((error) => {
console.log(error);
return false;
});
}
我认为
isCurrentUserHavingThisItem
工作正常。但是,当我按下“添加项目”时,我会触发以下函数:
const addItem = (route, id) => {
firebase.firestore()
.collection('Users')
.doc(id)
.collection('Items')
.doc(route.params.item.key)
.set({})
.then(setAdd(true));
}
这给了我以下错误:
ReferenceError:找不到变量:setAdd
我也尝试了以下操作:
const addItem = (route, id) => {
firebase.firestore()
.collection('Users')
.doc(id)
.collection('Items')
.doc(route.params.item.key)
.set({});
setAdd(true);
}
但它给了我同样的错误。以下是整个组件结构,以便更好地查看:
const SimpleComponent = ({ route }) => {
const { id } = route.params;
const [add, setAdd] = useState(false);
useEffect(() => {
{ isCurrentUserHavingThisItem(route, id) ?
setAdd(false)
:
setAdd(true)
}
}, [])
return (
<View>
{ add ?
(
<TouchableOpacity onPress={() => removeItem(route, id)}>
<Text style={styles.title}>Remove item</Text>
</TouchableOpacity>
)
:(
<TouchableOpacity onPress={() => addItem(route, id)}>
<Text style={styles.title}>Add item</Text>
</TouchableOpacity>
)
}
</View>
);
};
const isCurrentUserHavingThisItem = (route, id) => {
//same as I described it above; I think it works fine
}
const addItem = (route, id) => {
//same as described above
//gives me error when I try to update the state
}
const leaveMeetup = (route, id) => {
//same as described above
//gives me error when I try to update the state
}
export default SimpleComponent;
基本上,在我成功添加项目后,我想显示“删除项目”按钮,反之亦然。为了做到这一点,我尝试更新状态,但正如我所说,我遇到了
ReferenceError: Can't find variable: setAdd
错误,我不知道为什么。有人可以帮忙吗?
1个回答
您的 setAdd 方法位于组件之外,这就是显示该错误的原因...请将您的方法移到功能组件内,如下所示。
const SimpleComponent = ({ route }) => {
const { id } = route.params;
const [add, setAdd] = useState(false);
const isCurrentUserHavingThisItem = (route, id) => {}
const addItem = (route, id) => {}
const leaveMeetup = (route, id) => {}
useEffect(() => {
{ isCurrentUserHavingThisItem(route, id) ?
setAdd(false)
:
setAdd(true)
}
}, [])
return (
<View>
{ add ?
(
<TouchableOpacity onPress={() => removeItem(route, id)}>
<Text style={styles.title}>Remove item</Text>
</TouchableOpacity>
)
:(
<TouchableOpacity onPress={() => addItem(route, id)}>
<Text style={styles.title}>Add item</Text>
</TouchableOpacity>
)
}
</View>
);
};
export default SimpleComponent;
我认为您不能这样做。
.then(setAdd(true));
您会在
.then()
中获得一个回调
因此,正确的方法是
.then(()=>{
setAdd(true)
}).
sakshya73
2021-04-01