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 set value initially? #533

Open
harika-zazz opened this issue Dec 18, 2023 · 2 comments
Open

How to set value initially? #533

harika-zazz opened this issue Dec 18, 2023 · 2 comments

Comments

@harika-zazz
Copy link

Describe the bug

I'm using this module it is working fine. I need to show value by default which is getting from API.

To Reproduce

I am using value prop but it's not working.

Expected behavior

How can i show value by default. What is the prop?

Additional details

  • Device: All
  • OS: All
  • react-native-picker-select version: 9.0.0
  • react-native version: 0.72.6

Reproduction and/or code sample

const [homebuild, setHomeBuild] = useState("");
  const picker = useRef<RNPickerSelect>(null);
    useEffect(() => {
  
      picker.current?.setState({ homebuild: { value: propertyBuildOnYear, label: propertyBuildOnYear.toString(), } });
    
  }, [])
 <RNPickerSelect
              ref={picker}
              placeholder={{
                label: "Home was build in *",
                value: null,
              }}
              onValueChange={(value) => {
                Keyboard.dismiss();
                setHomeBuild(value)
              }}
              items={yearsList}
              style={{
                ...pickerSelectStyles,
              }}
              // value={homebuild}
              useNativeAndroidPickerStyle={false}
              Icon={() => {
                return <Image source={icon.dropdownIcon} style={{ ...styles.dropdownIconStyle, tintColor: "grey" }} />;
              }}
            />
@harika-zazz
Copy link
Author

harika-zazz commented Dec 28, 2023

I've added the value and key prop in RNPickerSelect. The value was shown by default. But when i try to edit the Picker value, it was resetting for the first time. How can i fix this issue?

Code:-

  const picker = useRef<RNPickerSelect>(null);
  useEffect(() => {
  
      const {
        propertyBuildOnYear,
      } = route?.params?.property;
      setHomeBuild({ value: propertyBuildOnYear, label: propertyBuildOnYear.toString(), })
      picker.current?.setState({ value: propertyBuildOnYear, label: propertyBuildOnYear.toString() });
    
  }, [])
<RNPickerSelect

              placeholder={{
                label: "Home purchased in *",
                value: null,
                color: colors.defaultTxtColor,
                fontFamily: fontFamilyType.HND.Regular,

              }}
              onValueChange={(value) => {
                Keyboard.dismiss();
                setHomePurchage(value)
              }}
              key={homepurchage.value}
              value={homepurchage.value}
              items={yearsList}
              style={{
                ...pickerSelectStyles,
                placeholder: {
                  color: colors.defaultTxtColor,
                  fontSize: scaler(16),
                  fontFamily: fontFamilyType.HND.Regular
                },
                iconContainer: {
                  top: 15,
                  right: 15,

                },
              }}
              useNativeAndroidPickerStyle={false}
              Icon={() => {
                return <Image source={icon.dropdownIcon} style={{ ...styles.dropdownIconStyle, tintColor: "grey" }} />;
              }}
            />

Screen recording:-

Screen.Recording.2023-12-28.at.3.00.13.PM.mov

@valentin-debris
Copy link

I had the same problem with a form, everytime I changed a field, the select was reset to the initial value.

The solution is to update the variable you used in 'value' , in the onValueChange

    const [defaultV, setDefaultV] = useState();

    return (
        <View>
            <RNPickerSelect
                items={data}
                value={defaultV}
                onValueChange={(value) => {
                    setDefaultV(value);
                }}
            />
        </View>
    );

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

2 participants