Kaan Karamanoğlu
Kaan Karamanoğlu

Reputation: 189

How do I set a value as a string React-Native?

I am setting up a new token value with react native fetch method and ı take the token value which is 'AGFsdkhgdgdkFSADLfksdgk' for example. I want to use this token with string value how ?

I am also tried get and set method but it is not necessary. For example ı have a string value which is '_token'. I want to use this _token value every class when ı using get method for reading data.

Here is my fetch method function.

_getToken () {
    const data = {
        grant_type: "",
        branchcode: "",
        password: "",
        username: "",
        dbname: "",
        dbuser: "",
        dbpassword: "",
        dbtype: ""
    }
    return (
        fetch('http:/192.168.1.29:7070/api/v2/token', {
            method: 'POST',
            body: qs.stringify(data)
        }).then(response => response.json())
            .then(responseData => {
                console.log(responseData['access_token']);
            })
            .catch(function (error) {
                console.log("HATA!!!" + error);
            })
    );
}

Upvotes: 1

Views: 1417

Answers (1)

Reza Mazarlou
Reza Mazarlou

Reputation: 3156

you can use AsyncStorage for store access token like this :

import {AsyncStorage} from 'react-native';

AsyncStorage.setItem('token',responseData['access_token']);

and use it like this :

  AsyncStorage.getItem('token').then((value)=>{
 console.log(value) //value is stored token
})

Upvotes: 2

Related Questions