【问题标题】:React native Dropdownalert with Navigation not working together in Redux SagaReact Native Dropdownalert with Navigation 在 Redux Saga 中无法协同工作
【发布时间】:2021-08-29 13:29:05
【问题描述】:

我正在使用 react-native-dropdownalert 和导航。两者都在 App.js 中使用 ref 引用并且工作正常。

          <AppNavigation
         ...
            ref={navigatorRef => {
              NavigationService.setTopLevelNavigator(navigatorRef);
            }}
         ...
          </AppNavigation>
          <DropdownAlert 
          ...
            ref={ref => {DropDownHolder.setDropDown(ref)} }
          ...

但是,在 Saga 中使用时,它们不会一起工作。一旦使用 DropDown 显示警报,则导航永远不会发生。我试图同时使用 yield put 和 call 但结果是一样的。

export function* someAsyncSaga({ params, api, constant }) {
  try {

    // api = Api.someCall in that case
    const response = yield call(api, params)

//    "some_call_success"
    yield put({
      type: constant.SUCCESS, payload: response
    });

    if (response['Status'])  {


        yield put(DropDownHolder.getDropDown().alertWithType(
          'success',
          '',
          'Alert showing just fine',
      ))

        // But navigation never happens
        yield put(NavigationService.navigate('SomePage'))                  

    }
    
  } catch (error) {
    console.log(error, 'error')
    yield put({
      type: constant.ERROR, payload: error
    });
  }
}

但如果我使用 componentDidUpdate 我认为这是一种不好的做法,我可以看到警报并进行导航。

    // Lifecycle methods
    componentDidUpdate(prevProps) {

if (this.props.Order != prevProps.Order) {

                    // Alert showing just fine
                    DropDownHolder.getDropDown().alertWithType(
                      'success',
                      '',
                      'Alert showing just fine',
                    )
                    
                    // Navigation happens just fine
                    this.props.navigation.navigate('SomePage')

            }

    }

尤其是我什至能够进行导航,然后显示警报。

if (this.props.Order != prevProps.Order) {
                    
                    // Navigation happens just fine
                    this.props.navigation.navigate('SomePage')


                    // Alert showing just fine after navigation
                    DropDownHolder.getDropDown().alertWithType(
                      'success',
                      '',
                      'Alert showing just fine',
                    )


            }

但是对于 saga,我只能进行导航或 DropDownAlert,但不能一起使用。 如何在我的 saga 中同时使用 DropDownAlert 和导航?

【问题讨论】:

    标签: javascript reactjs react-native redux redux-saga


    【解决方案1】:

    我在传奇中使用 NavigationService 的方式存在问题。实际错误是put(action): argument action is undefined,解决该问题的方法是这样调用 NavigationService:

    yield call(() =&gt; NavigationService.navigate('SomePage'))

    在github上找到答案https://github.com/react-navigation/react-navigation/issues/840#issuecomment-450278415

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2018-11-14
      • 2017-11-28
      相关资源
      最近更新 更多