Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use updateIsActiveIndex #136

Open
t-castanho opened this issue Dec 16, 2019 · 4 comments
Open

How to use updateIsActiveIndex #136

t-castanho opened this issue Dec 16, 2019 · 4 comments

Comments

@t-castanho
Copy link

I tried with a ref.
But every time i want to load the value i'm getting that this.ref.radioform.updateIsActiveIndex is not an object and the value isn't updated

@guosim
Copy link

guosim commented Dec 17, 2019

Try following the example under Callback Refs. It worked for me.
https://reactjs.org/docs/refs-and-the-dom.html

@BolajiOlajide
Copy link

This is an example

const Settings = () => {
  const radioButtonRef = useRef(null);
  const [settings, updateSettings] = useAsyncStorage(USER_SETTINGS);

useEffect(() => {
    if (settings) {
      const monitorOptionIdx = TRACKING_CHOICES.indexOf(settings.trackingOption);
      radioButtonRef.current.updateIsActiveIndex(monitorOptionIdx);
      setMonitoringOption(monitorOptionIdx);
      setThreshold(settings.threshold.toString());
      setUserCoinsToTrack(settings.coinsToTrack);
    }
  }, [settings]);

  return (
   <View style={styles.optionBlock}>
          <Text style={styles.settingsText}>
            How would you like to monitor prices?
          </Text>
          <RadioForm
            initial={monitoringOption}
            ref={radioButtonRef}
            radio_props={monitoringOptions}
            onPress={(value) => setMonitoringOption(value)}
            buttonColor={colors.BLACK}
            labelColor={colors.BLACK}
          />
        </View>
  );
}

@victorwvieira
Copy link

This is an example

const Settings = () => {
  const radioButtonRef = useRef(null);
  const [settings, updateSettings] = useAsyncStorage(USER_SETTINGS);

useEffect(() => {
    if (settings) {
      const monitorOptionIdx = TRACKING_CHOICES.indexOf(settings.trackingOption);
      radioButtonRef.current.updateIsActiveIndex(monitorOptionIdx);
      setMonitoringOption(monitorOptionIdx);
      setThreshold(settings.threshold.toString());
      setUserCoinsToTrack(settings.coinsToTrack);
    }
  }, [settings]);

  return (
   <View style={styles.optionBlock}>
          <Text style={styles.settingsText}>
            How would you like to monitor prices?
          </Text>
          <RadioForm
            initial={monitoringOption}
            ref={radioButtonRef}
            radio_props={monitoringOptions}
            onPress={(value) => setMonitoringOption(value)}
            buttonColor={colors.BLACK}
            labelColor={colors.BLACK}
          />
        </View>
  );
}

This example worked perfectly for me. Tks!

@carmiaca
Copy link

carmiaca commented Feb 25, 2021

I looked all over the web for how to change the initial value of a radio button with an asynchronous form update from the local device saved information.
radioButtonRef.current.updateIsActiveIndex(myindex) did not work for me. So I gleaned what I needed from the code and am using instead the react-native-simple-radio-button function is_active_index found in index.js:

constructor(props) {
super( props );
this.radioButtonRef = React.createRef();
}

make a function to loop through the button props and when a you find a match for the value you want to select:
changeInitialValue = ( newbuttonvalue, buttonRef, buttonProps ) => {
if( newbuttonvalue != null ) {
for( var key in buttonProps ) {
if( buttonProps[key].value == newbuttonvalue ) {
//won't work without parseInt - views key as string instead of number
buttonRef.current.setState({ is_active_index: parseInt( key ) });
} } } }

Refer to the function in your asynchronous get to set the new value in a .then((
this.changeInitialValue( returnArray.mybuttonnewvalue, this.radioButtonRef, this.button_props );

in the form...I'm using Formik...
if using Formik, you must use...enableReinitialize
<Formik
enableReinitialize={true}
......
<RadioForm
ref={this.radioButtonRef}
value={values.program}
radio_props={this.button_props}
initial={-1}
....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants