React native 如何使用React Native将上下文绑定到异步存储
我有一份物品清单。用户可以喜欢它们。 我使用AsynStorage来存储状态。它起作用了。 只是,我无法将存储绑定到特定项。如果我喜欢/不喜欢某个项目,所有项目都会发生同样的情况。这是代码,一个想法React native 如何使用React Native将上下文绑定到异步存储,react-native,firebase-realtime-database,asyncstorage,React Native,Firebase Realtime Database,Asyncstorage,我有一份物品清单。用户可以喜欢它们。 我使用AsynStorage来存储状态。它起作用了。 只是,我无法将存储绑定到特定项。如果我喜欢/不喜欢某个项目,所有项目都会发生同样的情况。这是代码,一个想法 componentWillMount() { this._renderSwitchButtonWithAsyncStorage().done; } _renderSwitchButtonWithAsyncStorage = async() => { let tok
componentWillMount() {
this._renderSwitchButtonWithAsyncStorage().done;
}
_renderSwitchButtonWithAsyncStorage = async() => {
let token = await AsyncStorage.getItem('AlreadyLiked');
if (token){
this.setState({trueSwitchIsOn: true});
}else{
this.setState({trueSwitchIsOn: false});
}
};
onPressIcon(word){
AsyncStorage.setItem('AlreadyLiked', JSON.stringify({trueSwitchIsOn}));
}
使用不同的异步存储密钥名称, 如果您使用相同的名称影响所有用户,因此每个用户都使用了不同的键,我知道了,谢谢:
onPressIcon(word){
const { _key} = this.props.navigation.state.params;
AsyncStorage.setItem(JSON.stringify({_key}),JSON.stringify({trueSwitchIsOn}));
//Check if token exists
_renderSwitchButtonWithAsyncStorage = async() => {
const {_key} = this.props.navigation.state.params;
let token = await AsyncStorage.getItem(JSON.stringify({_key}));
您需要设置一个不同的“键”,作为第一个参数的字符串传入。这样,当您将该值设置为其他值时,它只会受到该值的影响key@MattAft,你能告诉我更多的细节吗?我曾尝试过传递(word),比如so
onPressIcon(word){AsyncStorage.setItem(JSON.stringify(word),JSON.stringify({trueSwitchIsOn}));
但无法正常工作好吧,非常新手,你能给我更多细节吗?我尝试过传递(word),比如soonPressIcon(word){AsyncStorage.setItem(JSON.stringify(word),JSON.stringify({trueSwitchIsOn}));
但无法使其工作