0

Как значение с одной страницы можно передать на другую (для применения настроек в приложении, к примеру)?

Наткнулся на AsyncStorage. Пытаюсь его применить + прочитать в консоли, но выходит всегда такая ошибка, при переключении switch. Понимаю, что наверное надо вызывать try { await и т.д., но не пойму, как в компоненте обращаться к нему.

Redux не применяю, страницы реализованы через react-navigation.

введите сюда описание изображения

    import React, { useState } from 'react';
    import { View, Text, TouchableOpacity, ImageBackground, Platform, AsyncStorageStatic } from 'react-native';
    import {StatusBar} from 'react-native';
    import { stylebg } from '../styles/Styles';
    import { gstyles } from '../styles/Styles';
    import { Bgurl } from '../styles/Styles';
    import SwitchSelector from "react-native-switch-selector";
    import AsyncStorage from '@react-native-async-storage/async-storage';

    export default function Settings({navigation}) {
        const LoadScene = () => {
            navigation.navigate('Main');
        }
        const [switchValue, setSwitchValue] = useState('0');
        const toggleSwitch = (options) => {
            setSwitchValue(options);
            let value = switchValue;
            AsyncStorage.setItem('Bar', value);
            console.log (AsyncStorage.getItem('Bar'));
        };
        const options = [
            { label: "OFF", value: '0' },
            { label: "ON", value: '1' }
        ];
        

        if (Platform.OS === 'ios') {
            return (
                <ImageBackground blurRadius={15} source={Bgurl} resizeMode="cover" style={stylebg.image}>
                    <View style={gstyles.SettingsFirst}>

                        <View>
                            <StatusBar 
                                barStyle={'dark-content'} //light-content or dark-content
                                hidden={AsyncStorage.getItem('Bar') === '1' ? 'false' : 'true'}
                            />
                        </View>

                        <View style={gstyles.SettingsAllWithoutSave}>

                            <View style={switchValue ? gstyles.ViewStatusBarON : gstyles.ViewStatusBarOFF}>

                                <Text style={gstyles.textInSettingsTitle}>Settings</Text>

                                <Text style={gstyles.textStatusBarTurn}>
                                    {switchValue ? 'Status Bar' : 'Status Bar'}
                                </Text>

                                <View style={gstyles.switch}>
                                    <SwitchSelector style={gstyles.switchStatusBar}
                                        onPress={toggleSwitch}
                                        options={options}
                                        initial={0}
                                        selectedColor={'black'}
                                        buttonColor={'white'}
                                        backgroundColor={'rgba(0, 0, 0, 0.5)'}
                                        textColor={'rgba(255, 255, 255, 0.5)'}
                                        fontSize={18}
                                        textStyle={gstyles.switchText}
                                        selectedTextStyle={gstyles.switchTextSelected}
                                        animationDuration={200}
                                    />
                                </View>

                            </View>

                        </View>

                        <View style={gstyles.centrIOSSett}></View>

                        <View style={gstyles.aroundbuttonSaveSettings}>
                            <TouchableOpacity style={gstyles.ButtonInSettingSave} onPress={LoadScene}>
                                <Text style={gstyles.ButtonInSettingSaveText}>Save</Text>
                            </TouchableOpacity>
                        </View>

                    </View>
                </ImageBackground>
            );
        }
        else {
            //Тут тоже самое, но для Android
        }
    }

1 ответ 1

0

Попробуйте так:

AsyncStorage.getItem('token').then((response) => {console.log(response)});
2

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.