如何将选定的单选按钮值存储在 React Native 的 AsyncStorage 中?

How to store chosen radio button value in a AsyncStorage in React Native?

我想 store/save 用户在 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 的 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}
        />
  )
}