实际上,我是redux概念的新手。我正在使用某些登录应用程序,因此我必须将用户信息存储在redux状态。我正在整个应用程序中使用此用户信息。但是在移动应用程序中,如果我们终止了该应用程序并重新启动,则应该从redux状态中获取obj。为此,我想使用redux将此对象存储在异步存储中。谁能帮我
代码:
const initialState = {
userinfo : {}
}
const UserInfoReducer = (state = initialState,action) => {
switch(action.type){
case 'SAVE_TOKEN' :
return {state, userinfo: action.payload}
case 'RETRIEVE_TOKEN':
return state
}
return state
}
export default UserInfoReducer;
saveTokenMethod = async() => {
var tokenInfo = {
token : this.state.username
}
this.props.saveToken(tokenInfo)
}
const mapDispatchToProps = (dispatch) => {
return {
saveToken : (tokenInfo) => dispatch({type : 'SAVE_TOKEN',payload : tokenInfo})
}
}
答案 0 :(得分:1)
由于您正在使用Redux,因此可以使用let textAnimator = UIViewPropertyAnimator(duration: duration, timingParameters: linearTiming)
textAnimator.addAnimations {
switch state{
case .expanded:
self.tempLabel.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
self.tempLabel.frame = CGRect(origin: CGPoint(x: 15, y: self.diff / 2 - self.tempLabel.frame.height / 2), size: self.tempLabel.frame.size)
case .collapsed:
self.tempLabel.transform = CGAffineTransform.identity
self.tempLabel.frame = CGRect(origin: CGPoint(x: 15, y: self.view.frame.height / 2 - self.tempLabel.frame.height / 2 - 30), size: self.tempLabel.frame.size)
来处理设备的持久状态,默认情况下,它将使用redux-persist
来存储状态。您还可以使用AsyncStorage
选择应该保留哪些redux,哪些不应该保留。
答案 1 :(得分:1)
在应用启动时检索存储数据
答案 2 :(得分:0)
尝试
First create a reducerfile Index(like index.js)
import { combineReducers } from 'redux';
import tokenReducer from ./savetoken.js;
export default combineReducers({
mytoken:tokenReducer
})
1) Reducer function(savetoken.js)
const initialState = {
userinfo : {}
}
export default (state = initialState,action) => {
switch(action.type){
case 'SAVE_TOKEN' :
return {state, userinfo: action.payload}
case 'RETRIEVE_TOKEN':
return state
}
return state
}
}
2) Action Function
export const saveTokenMethod = (value) => {
return{
type:'SAVE_TOKEN',
payload:value
}
}
3) LoginPage (Saving token part)..let take loginpage.js
import * as actions from '../../../../actions';(Give your action page path here)
Class LoginPage{
.....
this.props.saveTokenMethod(token);
var user_token = this.props.mytoken;
AsyncStorage.setItem('logintoken',user_token );
}
const mapStateToProps = state => {
return{
mytoken:state.mytoken
}
}
export default connect(mapStateToProps,actions})(LoginPage)
答案 3 :(得分:0)
转换您的redux状态 JSON.stringify(obj);并存储在 AsyncStorage