简体   繁体   English

在反应原生钩子中刷新 function 的拉动等效于什么?

[英]What's the equivalent of this pull to refresh function in react native hooks?

   _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();
  }

i got this code that i need to convert into react hooks code, i know pretty much everything related to useEffect() except for this focuslistener and componentwillunmount thing, and i got the same code in componentdidmount, but i think i know how to do that one我得到了需要转换成反应钩子代码的代码,我知道几乎所有与 useEffect() 相关的东西,除了这个 focuslistener 和 componentwillunmount 的东西,我在 componentdidmount 中得到了相同的代码,但我想我知道该怎么做一

You should replace the code with something like below, Here the loadItems function loads items from your service or backend this function will be called from the focus effect and also the refresh.您应该用类似下面的代码替换代码,这里 loadItems function 从您的服务或后端加载项目,此 function 将从焦点效果和刷新中调用。

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 />;
}

This code is not tested as it requires your functions but will give you the idea.此代码未经测试,因为它需要您的功能,但会给您一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM