# How to convert this code to cljs

**URL:** https://clojureverse.org/t/how-to-convert-this-code-to-cljs/4166
**Category:** Beginners
**Created:** [April 19, 2019, 12:06pm UTC](https://clojureverse.org/t/how-to-convert-this-code-to-cljs/4166 "2019-04-19T12:06:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vinurs](https://clojureverse.org/user_avatar/clojureverse.org/vinurs/32/5341_2.png) [@vinurs](https://clojureverse.org/u/vinurs)
#### Post date: [April 19, 2019, 12:06pm UTC](https://clojureverse.org/t/how-to-convert-this-code-to-cljs/4166/1 "2019-04-19T12:06:04Z")

</div>

i want to use this library [react-native-storage](https://github.com/sunnylqm/react-native-storage) in my cljs project, and write the code:

```
(def storage (new Storage #js {:size 1000
                               :storageBackend js/window.localStorage
                               :defaultExpires (* 1000 3600 24)
                               :enableCache true}))
(.save storage #js {:key :username :data "vinurs"})
(.load storage #js {:key :username})

```

this is the load function it provided:

```
// load
storage
  .load({
    key: 'loginState',

    // autoSync (default: true) means if data is not found or has expired,
    // then invoke the corresponding sync method
    autoSync: true,

    // syncInBackground (default: true) means if data expired,
    // return the outdated data first while invoking the sync method.
    // If syncInBackground is set to false, and there is expired data,
    // it will wait for the new data and return only after the sync completed.
    // (This, of course, is slower)
    syncInBackground: true,

    // you can pass extra params to the sync method
    // see sync example below
    syncParams: {
      extraFetchOptions: {
        // blahblah
      },
      someFlag: true
    }
  })
  .then(ret => {
    // found data go to then()
    console.log(ret.userid);
  })
  .catch(err => {
    // any exception including data not found
    // goes to catch()
    console.warn(err.message);
    switch (err.name) {
      case 'NotFoundError':
        // TODO;
        break;
      case 'ExpiredError':
        // TODO
        break;
    }
  });

```

i don’t know how to convert it to cljs, and load the key’s value in cljs

---

<div class="post-metadata">

### Author: ![vinurs](https://clojureverse.org/user_avatar/clojureverse.org/vinurs/32/5341_2.png) [@vinurs](https://clojureverse.org/u/vinurs)
#### Post date: [April 19, 2019, 1:49pm UTC](https://clojureverse.org/t/how-to-convert-this-code-to-cljs/4166/2 "2019-04-19T13:49:49Z")

</div>

```
;; (def storage (new Storage #js {:size 1000
;; :storageBackend js/window.localStorage
;; :defaultExpires (* 1000 3600 24)
;; :enableCache true}))
;; (.save storage #js {:key :username :data "vinurs"})
;; (def ret (.load storage #js {:key :username}))
;; (.then ret #(print %))

```

it can be get out like this

---

<div class="post-metadata">

### Author: ![tomc](https://clojureverse.org/user_avatar/clojureverse.org/tomc/32/1990_2.png) [@tomc](https://clojureverse.org/u/tomc)
#### Post date: [April 19, 2019, 3:36pm UTC](https://clojureverse.org/t/how-to-convert-this-code-to-cljs/4166/3 "2019-04-19T15:36:24Z")

</div>

It’s not clear what part you’re struggling with, but, knowing nothing about react-native-storage, it seems possible that `key` should be a string, rather than a keyword. Also, if you supply the `syncParams` option be sure to add a #js before it.

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [October 19, 2019, 3:36am UTC](https://clojureverse.org/t/how-to-convert-this-code-to-cljs/4166/4 "2019-10-19T03:36:25Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
