【发布时间】:2021-06-19 14:21:47
【问题描述】:
我发现了许多类似的问题,但大多数解决方案都没有 redux 解决方案。我想在从 redux 商店删除项目后调用订阅。
以下是我取消所有订阅的代码,但我不确定我的做法是否正确!
const useFetchProperty = (propertyId, refresh = false) => {
const dispatch = useDispatch();
const property = useSelector(state => selectPropertyById(state, propertyId));
const isConnected = useIsConnected();
useEffect(() => {
let isSubscribed = true;
if (
isSubscribed &&
isConnected &&
(property === undefined || refresh === true)
) {
dispatch(fetchProperty({propertyId: propertyId}));
}
return () => (isSubscribed = false);
}, [dispatch, refresh, isConnected, property, propertyId]);
return property;
};
export default useFetchProperty;
CarDesign.js
const CardDesign = ({postId}) => {
const {t} = useContext(LocalizationContext);
const post = useFetchProperty(postId);
const currentLanguage = useSelector(currentAppLanguage);
const navigation = useNavigation();
//the rest show data would be here
}
你会看到我正在使用这两个
let isSubscribed = true; 和 return () => (isSubscribed = false);。用dispatch取消所有订阅和异步任务是否正确???
【问题讨论】:
标签: reactjs react-native redux