_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] 删除。
我来说两句