Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

javascript - 反应js localstorage值对象[重复](react js localstorage value object [duplicate])

I save what I get from my backend to a local storage:+(我将从后端获得的内容保存到本地存储中:+)

   async onSubmit(e){
        e.preventDefault();
        const {login, password } = this.state;

        const response = await api.post('/login', { login,password });
        const user = response.data.user;
        const {jwt} = response.data;
        console.log(user);
        localStorage.setItem('token', jwt);
        localStorage.setItem('user', user);
        this.props.history.push("/home");
    }

my(我的)

const user = response.data.user;(const用户= response.data.user;)

return this:(返回此:)

{id: 2, name: "spt", email: "email", login: "spt", password: "$2a$10$Rqc1VU1TfKD6MypNzbgemeR0O4YeXIFy1XiURjNeHk0gpWJitp4da", …}

two object [object object](两个对象[object object])

  ask by Felipe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Local Storage is key-value storage where key is string and value is string also.(本地存储是键值存储,其中键是字符串,值也是字符串。)

You must stringify your data, you can do this(您必须对数据进行字符串化,才能执行此操作)

localStorage.setItem('user', JSON.stringify(user));

And get it like(并得到像)

const user = JSON.parse(localStorage.getItem('user'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...