Skip to content

localStorage로 object 값 사용하기 #3

@2myungho

Description

@2myungho

localStorage를 사용하는데 [object][object], [object][object], [object][object] 이런식으로 데이터가 들어와서 당황했다.

해결 방법은 객체를 저장하기 전에 문자화 하고, 사용할 때 구문을 분석하는 것이다.

/ /Groupstore 로컬스토리지에 내 그룹 저장  

localStorage.setItem('myGroups', JSON.stringify(this.myGroups)); //키, 값  
// HeaderMainContainer  header에서 로컬스토리지에 저장된 데이터 사용  

const { group } = this.props.Store;  
var myGroup_object = localStorage.getItem('myGroups')  
group.myGroups_array(JSON.parse(myGroup_object));  

데이터가 잘 저장 된다.  
0: {id: 1, imgUrl: null, title: "기술 ", description: "기술페이지입니다.", category: "technology", master: "master",…}  
1: {id: 2, imgUrl: null, title: "여행", description: "여행카테고리", category: "travel", master: "master",…}  
2: {id: 3, imgUrl: null, title: "비즈니스", description: "비즈니스 페이지입니다.", category: "business",…}  
3: {id: 7, imgUrl: null, title: "스포츠", description: "스포츠입니다.", category: "sports", master: "master",…} 

참고 사이트

https://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions