【问题标题】:How to store chosen radio button value in a AsyncStorage in React Native?如何将选择的单选按钮值存储在 React Native 的 AsyncStorage 中?
【发布时间】:2021-06-03 16:11:02
【问题描述】:

我想在 AsyncStorage 中存储/保存用户选择的单选按钮值。这是如何实现的?我正在从另一个文件中检索单选按钮值并将它们分配给标签。这些是我的单选按钮的结构:

import RadioButtonRN from 'radio-buttons-react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

const Radio = () => {
  const label1 = Questions[2].answers[0].answer;
  const label2 = Questions[2].answers[1].answer;
  const label3 = Questions[2].answers[2].answer;

  const data = [
    {
      label: label1,
    },
    {
      label: label2,
    },
    {
      label: label3,
    },
  ];

  return (
    <RadioButtonRN
      data={data}
      selectedBtn={(e) => console.log(e)}
      box={false}
      circleSize={14}
      activeColor={'#6175CF'}
      style={styles.radio}
    />
  );
};

export default Radio;

【问题讨论】:

    标签: javascript reactjs react-native radio-button asyncstorage


    【解决方案1】:

    您可以直接使用 javascript 的 localStorage 来存储和获取会话中的值。 setSelectedRadioBtn 通过名称radioBtn 设置存储中的值,getSelectedRadioBtn 获取存储中存储的值

    setSelectedRadioBtn = (selectedBtn) => localStorage.setItem('radioBtn', selectedBtn);
    
    getSelectedRadioBtn = () => console.log(localStorage.getItem('radioBtn'));
    
    return (
            <RadioButtonRN
              data={data}
              selectedBtn={(e) => setSelectedRadioBtn(e)}
              box={false}
              circleSize={14}
              activeColor={'#6175CF'}
              style={styles.radio}
            />
      )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 2022-12-11
      • 2021-09-30
      • 1970-01-01
      • 2020-07-12
      相关资源
      最近更新 更多