expo / react-native-action-sheet

A cross-platform ActionSheet for React Native
MIT License
1.4k stars 226 forks source link

callback not refreshing after calling it first time. #276

Closed SymntxHomendra51 closed 1 year ago

SymntxHomendra51 commented 2 years ago

In my case in android the callback is not refreshing after first call It was returning same values even after i call with state which is changed. Here formikValues is a state. It returns right object on first call but when i reselect after changing the formikvalues object it takes the get old formikvalues used in the first call.
I also tried calling this function outside of showactionSheetWithOptions it's working as expected.

Here is the code

 const showTemplateList = async (tapAction: (arg0: any) => any) => {
    const tempList = await getTemplateList()
    tempList.push({ label: 'Cancel' })
    console.log('templist', tempList)
    showActionSheetWithOptions(
      {
        // title: 'Save',
        options: tempList?.map(item => item.label),
        cancelButtonIndex: tempList?.length - 1,
        // destructiveButtonIndex: options?.length,
        destructiveColor: 'red',
        showSeparators: true,
        containerStyle: {
          maxHeight: '90%',
        },
      },
      index => {
        console.log(tempList[index])
        if (tempList[index]?.temp_id >= 0) tapAction(tempList[index]?.temp_id)
      },
    )
  }

  const setTemplate = async (temp_id: number) => {
    console.log('set formik values', formikValues)

    const jsString = JSON.stringify({ ...formikValues })
    const template = {
      temp_id: temp_id,
      temp_json: jsString,
      temp_b_id: 1,
    }
    console.log('jsstring', template)
    saveTemplatesItem([template])
  }

showTemplateList(setTemplate)`
bradbyte commented 2 years ago

Hi @SymntxHomendra51... I'm sorry but I'm not really able to follow the issue from the code. However, keep in mind calling showActionSheetWithOptions is static and not bound to react state, so you probably just have an issue where the object receives the initial formikState, and that's it. You would need to always pass in the new formik state to the method to get the current values.

Essentially, whenever formik rerenders it will create new instances of your setTemplate function, however, the static action sheet method only got the first one. I would move your showTemplateList to a useCallback and pass in the formik values as an argument each time it's invoked.