我有以下状态类:
import { observable, action } from 'mobx';
import axios from 'axios';
export default class AppState {
@observable user;
@observable pToken;
constructor() {
this.user = {};
this.pToken = localStorage.getItem('pToken');
}
async fetchData(query) {
const body = JSON.stringify({ query, });
const response = await axios.post('url', body, {
headers: {
'Content-Type': 'application/json',
token: localStorage.getItem('pToken')
}
});
const user = response.data.data.user;
console.log('Got user', user);
this.setUser(user);
}
@action setUser(user) {
this.user = user;
}
}
在我的组件中:
@inject('store')
@observer
export default class Header extends Component {
constructor(props) {
super(props);
this.store = this.props.store.appState;
}
render() {
const { user } = this.store;
console.log('store', this.store);
return (
<header className='header'>
User - {user.username}
</header>
);
}
}
不幸的是,状态user
属性返回一个Proxy
对象,而user
日志显示用户对象。知道我错过了什么吗?