React Uncaught SyntaxError: Unexpected token u in JSON at position 0

做react项目时,用到了mobx,登录出现该错误,查询后发现是说
JSON.parse解析了undefined
那么回头看看我们的代码,发现了问题

原代码:

@observable user = localStorage.getItem('user')?JSON.parse():[];
    @observable isLogin = false;
    @observable token = localStorage.getItem('token')?JSON.parse():[];

改后代码:

@observable user = localStorage.getItem('user')?JSON.parse(localStorage.getItem('user')):[];
    @observable isLogin = false;
    @observable token = localStorage.getItem('token')?JSON.parse(localStorage.getItem('token')):[];

就成功登录了

你可能感兴趣的:(react)