最近,我正在使用Mobx,工作得很好,但当刷新页面时,数据丢失了,这是正常的,但我想在localStorage中保留数据。Mobx-persist用于在localStorage中保存数据,我在我的项目中实现了它,但它不起作用,我也不知道为什么。
这是我的authStore:
import { observable, action } from 'mobx';
import { fireauth } from '../config/firebase';
import { persist } from 'mobx-persist'
class AuthStore {
@persist @observable authState = false;
@persist @observable title = "Dashboard";
@persist @observable img = "";
@persist('object') @observable userAuth = {useremail: "", userpass: ""};
@action handleChange = (value, event) => {
this.userAuth[value] = event.target.value;
}
@action submit = () => {
fireauth.signInWithEmailAndPassword(this.userAuth["useremail"], this.userAuth["userpass"]).then(() => {
this.authState = true;
console.log(this.authState);
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log({ errorCode, errorMessage })
// ...
});
this.authState = true;
alert(this.authState)
}
@action logout = () => {
fireauth.signOut().then(() => {
this.authState = false;
console.log("si")
}).catch((error) => {
console.log(error)
});
}
}
export default AuthStore;我也有一个rootStore:
import { create } from 'mobx-persist';
import AuthStore from './authStore.js';
const hydrate = create({
storage: localStorage,
});
export class RootStore {
authStore = new AuthStore(this);
constructor() {
hydrate('auth', this.authStore)
}
};
export const RootStoreContext = new RootStore();我的索引传递根存储区:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Provider } from 'mobx-react';
import { RootStoreContext } from './store/rootStore';
ReactDOM.render(
<React.StrictMode>
<Provider rootStore={ RootStoreContext } authStore={ RootStoreContext.authStore }>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();和登录组件(在其中使用存储和应用操作)
import React from 'react';
import './index.css';
import logo from '../../assets/img/logo-ondoo.png';
import email from '../../assets/img/email-icon.svg';
import secure from '../../assets/img/secure-icon.svg';
import see from '../../assets/img/see-icon.svg';
import { inject, observer } from 'mobx-react';
@inject('authStore')
@observer
class App extends React.Component {
constructor(props) {
super(props);
}
render () {
return (
<div className="container">
<div className="login-form">
<div className="login-logo">
<img src={ logo }></img>
<p>Productividad al alcance de tu mano</p>
</div>
<div className="login-form-div">
<form >
<div className="text-input-content">
<img src={ email } id="username" name="username" className="text-input-lefticon"/>
<input type="text" placeholder="Correo electrónico" onChange={ (e) => this.props.authStore.handleChange("useremail", e)} />
</div>
<div className="text-input-content">
<img src={ secure } className="text-input-lefticon" />
<input type="text" id="password" placeholder="Contraseña" onChange={ (e) => this.props.authStore.handleChange("userpass", e)}/>
<img src={ see } className="text-input-righticon" />
</div>
<div>
<input className="button-enter" onClick={this.props.authStore.submit} type="button" value="Entrar"></input>
</div>
<div>
<input className="button-enter" onClick={this.props.authStore.logout} type="button" value="Entrar"></input>
</div>
</form>
</div>
</div>
</div>
);
}
}
export default App;我实现了水合物和所有的mobx-persist,但是localStorage没有保存数据。我一直在搜索互联网,但我找不到解决方案。
有人知道为什么会这样吗?
谢谢。
发布于 2020-11-29 07:57:44
这是因为在新版本的mobx中,mobx-persist不起作用。
我正在使用的版本:
mobx: ^3.4.1
mobx-persist: ^0.4.1
mobx-react: ^4.3.5而且效果很好。
https://stackoverflow.com/questions/64959863
复制相似问题