AdelRedaa97 / react-native-select-dropdown

react-native-select-dropdown is a highly customized dropdown | select | picker | menu for react native that works for andriod and iOS platforms.
MIT License
320 stars 137 forks source link

Selected Item not update text after forceUpdate #60

Closed dnguyen1987 closed 2 years ago

dnguyen1987 commented 2 years ago

Im trying to make a dropdown that support Multilanguage so I try to change the data array value by: var viewBy = [i18n.t("all"), i18n.t("branch"), i18n.t("corporate"), i18n.t("favorite"), i18n.t("online")] and my SelectDropDowm:

<SelectDropdown
                        ref={this.fitlerDropDown}
                        data={viewBy}
                        onSelect={(selectedItem, index) => {
                            this.updateFilterValue(selectedItem)
                        }}
                        buttonTextAfterSelection={(selectedItem, index) => {
                            return selectedItem
                        }}
                        rowTextForSelection={(item, index) => {
                            return item
                        }}
                        buttonStyle={workspaceStyle.dropdownButtonStyle}
                        buttonTextStyle={workspaceStyle.dropdownButtonTextStyle}
                        renderDropdownIcon={() => <DropDownIconSVG />}
                        defaultValueByIndex={0}
                        rowStyle={workspaceStyle.dropdownRowStyle}
                        rowTextStyle={workspaceStyle.dropdownRowTextStyle}
                    />

and after change language and back to this screen, I will force update for all screen by:

componentDidMount() {
        this._unsubscribe = this.props.navigation.addListener('focus', () => {
            viewBy = [i18n.t("all"), i18n.t("branch"), i18n.t("corporate"), i18n.t("favorite"), i18n.t("online")]
            this.forceUpdate()
        });
    }

    componentWillUnmount() {
        this._unsubscribe();
    }

Every other thing are fine but the selectedItem text in SelectDropDown didn't update (the dropdown list was updated) Could you help me for this please?

dnguyen1987 commented 2 years ago

I found a solution now. We just change

buttonTextAfterSelection={(selectedItem, index) => {
                            return selectedItem
                        }}

to buttonTextAfterSelection={(selectedItem, index) => { return viewBy[index] }} and it worked now. We can close this issue now Thanks !