본문으로 건너뛰기

SyncedStorage

데이터를 동기화하는 SyncedStorage 클래스입니다. 데이터가 업데이트될 때 리스너 함수를 호출합니다. ReactSyncConnector 와 연결하여 사용합니다.

Signature​

declare class SyncedStorage<Data> extends ReactSynced<Data> 

Extends: ReactSynced<Data>

Example​

const textStorage = new SyncedStorage<string>("text", localStorage)
const textConnector = new ReactSyncConnector(textStorage)

textStorage.set("Hello, World!")
textStorage.set((prev) => prev + "!")

textStorage.get() // "Hello, World!!"
textStorage.remove()

textStorage.get() // null

Constructors​

Constructor

Modifiers

Description

(constructor)(key, storage)

SyncedStorage 인스턴스를 생성합니다. 데이터를 저장할 키와 Storage 객체를 받습니다.

생성될때, storage 이벤트가 등록되며 다른 브라우저에서의 change event를 감지하여, 최신값을 가져옵니다.

Properties​

Property

Modifiers

Type

Description

get

() => Data | null

Storage에 저장된 json 데이터를 parse 한 후 가져옵니다.

key

string

remove

() => void

Storage에 저장된 데이터를 삭제합니다.

set

(data: DataOrFn<Data | null>) => void

Storage에 데이터를 저장합니다. 저장할 데이터 혹은 함수를 받아서 데이터를 저장합니다.