【发布时间】: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