Reactjs 为什么redux会持续';我不能保存我的数据吗?
我正在使用redux和redux persist在React Native for Android中制作一个应用程序。 我想用redux在我的应用程序中存储每个用户的ID,这样他们回来时就不需要再次登录。我制作了一个名为handleUser的reducer,允许用户登录。它在没有redux persist的情况下工作得很好,但是当我使用它时,当调用我的reducer时,存储不会更新。我不知道在iOS上是否也一样 我已经试过了:Reactjs 为什么redux会持续';我不能保存我的数据吗?,reactjs,redux,react-redux,react-native-android,redux-persist,Reactjs,Redux,React Redux,React Native Android,Redux Persist,我正在使用redux和redux persist在React Native for Android中制作一个应用程序。 我想用redux在我的应用程序中存储每个用户的ID,这样他们回来时就不需要再次登录。我制作了一个名为handleUser的reducer,允许用户登录。它在没有redux persist的情况下工作得很好,但是当我使用它时,当调用我的reducer时,存储不会更新。我不知道在iOS上是否也一样 我已经试过了:$rm-rf node_modules&&npm install,我还
$rm-rf node_modules&&npm install
,我还向我的rootPersistConfig添加了keyPrefix:“
,我在多个asking上找到了这个解决方案,但它不起作用。请注意,我使用了react导航,即使我认为它与此无关
这是我的配置存储:
import { createStore } from 'redux'
import handleUser from './Reducers/userReducer'
import { persistCombineReducers } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
const rootPersistConfig = {
key: 'root',
storage: storage,
keyPrefix: ''
}
export default createStore(persistCombineReducers(rootPersistConfig, { handleUser } ))
这是我的减速机:
const initialState = { ID: '' }
function handleUser(state = initialState, action) {
let nextState
console.log(action) //I added this debugger but the behaviour is expected, no error.
switch(action.type) {
case 'SIGN_IN':
nextState = {
...state,
ID: action.value.ID
}
return nextState || state
default:
return state
}
}
export default handleUser
下面是允许登录的组件:
import React from 'react'
import { StyleSheet, View, Button } from 'react-native'
import { connect } from 'react-redux'
class SignIn extends React.Component {
_signIn() {
const action = { type: 'SIGN_IN', value: { ID: 'my_id' } }
this.props.dispatch(action)
console.log(this.props) //shows ID as undefined when using redux-persist
}
render() {
return (
<View style={styles.mainContainer}>
<Button
title='Sign in'
onPress={ () => this._signIn() }
/>
</View>
)
}
}
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
alignItems: 'center'
}
})
const mapStateToProps = state => {
return {
ID: state.ID
}
}
export default connect(mapStateToProps)(SignIn)
从“React”导入React
从“react native”导入{样式表、视图、按钮}
从“react redux”导入{connect}
类签名扩展了React.Component{
_签名(){
const action={type:'SIGN_-IN',value:{ID:'my_-ID'}
本.道具.调度(动作)
console.log(this.props)//使用redux persist时将ID显示为未定义
}
render(){
返回(
这是.\u sign()}
/>
)
}
}
const styles=StyleSheet.create({
主容器:{
弹性:1,
对齐项目:“中心”
}
})
常量mapStateToProps=状态=>{
返回{
ID:state.ID
}
}
导出默认连接(MapStateTops)(登录)
我希望当我在登录组件中显示
this.props
时,调用reducer后,我会找到值my_id
。问题是它没有定义。控制台没有显示任何错误。我会检查一些东西
- 记录持久存储,查看它是否正在存储。 在RND控制台中键入showAsyncStorageContentInDev()。 或使用以下方式记录:
- 确保您正在使用redux persist提供的PersistGate
从'redux persist/integration/react'导入{PersistGate}
// ... 正常设置、创建存储和持久化、导入组件等。
常量应用=()=>{
返回(
);
};
如文档中所述。
问题已修复!身份证保存得很好,但我看不懂。感谢Alumium22,他给了我一个更好的阅读商店的方式。我创建了一个名为
GetStore.js
的新文件,其中包含一个在需要ID时使用的函数:
import AsyncStorage from '@react-native-community/async-storage'
export default function getStore(callback) {
return AsyncStorage.getAllKeys((err, keys) => {
return AsyncStorage.multiGet(keys, (error, stores) => {
const store = stores[1]
let parsedStore = JSON.parse(JSON.parse(store[1]).handleUser) //handleUser is my reducer
callback(parsedStore)
})
})
}
因此,当我想阅读商店时,我会:
import GetStore from 'your GetStore.js file path'
//...
GetStore( store => {
console.log(store)
})
我在哪里可以找到RND控制台(我搜索过,但它真的很混乱)?关于PersistGate,我已经在使用它了。React Native Debugger=RND(我认为它可能只是mac),该命令不会在典型的远程调试会话中执行。好的,我不在mac上。还有其他建议吗?我用一个简单的控制台记录persistor.log(),但我想它没用。k,我用另一种方法记录异步存储更新了我的答案。我觉得您的异步可能无法存储。谢谢您的提示!我从已安装的
@react native community/async storage
导入了异步存储。在分派后,我在函数\u-sign()
中记录了存储。我看到显示了两个存储,都显示my_ID
作为ID的值!所以我的async存储ID。我想我应该这样读取我的存储。我会试试看,如果有效的话,我会考虑修理的。
import AsyncStorage from '@react-native-community/async-storage'
export default function getStore(callback) {
return AsyncStorage.getAllKeys((err, keys) => {
return AsyncStorage.multiGet(keys, (error, stores) => {
const store = stores[1]
let parsedStore = JSON.parse(JSON.parse(store[1]).handleUser) //handleUser is my reducer
callback(parsedStore)
})
})
}
import GetStore from 'your GetStore.js file path'
//...
GetStore( store => {
console.log(store)
})