I am cleaning a component which listens to my db, and when it has a response, navigates to a specific screen.
This is my original component:
function MyComponent() {
const { navigation } = useNavigation();
const start = (data) => {
navigation.navigate("Somewhere", { data });
};
const listenData = (doc) => {
const { something } = doc.data();
start(something);
};
useEffect(() => {
const listener = listenDB((data) => {
setData(data);
};
return () => listener();
}, []);
return ...;
}
And I am thinking to move all the logic to a custom hook useMyComponentLogic();
function useMyComponentLogic() {
const { navigation } = useNavigation();
const start = (data) => {
navigation.navigate("Somewhere", { data });
};
const listenData = (doc) => {
const { something } = doc.data();
start(something);
};
useEffect(() => {
const listener = listenDB((data) => {
setData(data);
};
return () => listener();
}, []);
}
and then just use it in my component:
function MyComponent() {
useMyComponentLogic();
return <View>...</View>;
}
With this, my hook seems to do two things:
1. Handle navigation
2. Listening to db
Is it an anti-pattern to delegate all the responsibilities (like navigating when data is listened) to a custom hook?