react native hooks 中的 pull to refresh 功能相当于什么?

尼古拉斯·席尔瓦
   _onRefresh = async () => {
    this.setState({isRefreshing: true});
    await ExpenseReportHistoryBusiness.GetInstance().getListExpenseReportRequest();
    this.setState({isRefreshing: false});
    this.forceUpdate();
    this.focusListener = this.props.navigation.addListener(
      'didFocus',
      async () => {
        this.setState({displayMessage: true});
        await ExpenseReportHistoryBusiness.GetInstance().getListExpenseReportRequest();
        this.forceUpdate();
      },
    );
  };

  componentWillUnmount() {
    this.focusListener.remove();
  }

我得到了这段代码,我需要将其转换为 react hooks 代码,除了这个 focuslistener 和 componentwillunmount 之外,我几乎知道与 useEffect() 相关的所有内容,并且我在 componentdidmount 中得到了相同的代码,但我想我知道该怎么做一

古鲁帕兰

你应该用下面的代码替换代码,这里 loadItems 函数从你的服务或后端加载项目,这个函数将从焦点效果和刷新中调用。

function Scree1({ navigation }) {
  const [displayMessage, setDisplayMessage] = React.useState(false);

  const loadItems = async () => {
    setDisplayMessage(true);
    await ExpenseReportHistoryBusiness.GetInstance().getListExpenseReportRequest();
    setDisplayMessage(false);
  };

  React.useEffect(() => {
    const unsubscribe = navigation.addListener('focus', async () => {
      await loadItems();
    });

    // will replace the component will unmount
    return unsubscribe;
  }, [navigation]);

  return <View />;
}

这段代码没有经过测试,因为它需要你的函数,但会给你一个想法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 React/React Native 的 onPress 中拥有相当于 onClick 的功能?

在React中相当于React Native的FlatList

React Native 相当于 mousedown

相当于NSNotificationCenter for React Native吗?

使用react-native动画API与原点的react-native-svg旋转相当于什么

React Native相当于React.createElement

React JS相当于React Native {flex:1}?

在Android上相当于'SafeAreaView'-React Native

在 Reach 路由器中 React 路由器的 useHistory 钩子相当于什么

在react.js中相当于ng-if是什么?

使用React Native Hooks

为什么更新状态未反映在事件侦听器中:React Native,Hooks

React Native Hooks:无法在 useEffect 函数的 return() 中显示值

Java 7中的正则表达式替换功能评估相当于什么?

更新 map Function React 或 React-Hooks 或 React-Native 内的数组中的对象

相当于MS Access中NZ功能的ADO?

React 相当于 ejs

当使用 react-native-pull-to-refresh 包裹 ScrollView 组件时,scrollviewref.current.scrollToEnd() 不起作用

如何在提供的组件(尤其是带有fetchPopularMovies的部分)上实现这种依赖关系(react-native-pull-to-refresh)?

使用React Hooks进行React Native onLayout

纯C中的类相当于什么

google guice 中的 Spring 调度程序相当于什么?

iOS中的活动生命周期相当于什么?

在Perl中相当于$ | = 1的python是什么

Java流中Haskell的scanl相当于什么?

在Linux中相当于getch()和getche()是什么?

Express中的Laravel队列作业相当于什么?

什么相当于Objective-C中的sync?

C#中的Java枚举相当于什么?