Reputation: 79
Is there any way to use a custom react hook in my redux reducer?
I want to call getDistanceHandler() or useGetDistance() in my switch case, but React tells me that I'm not allowed to use hooks outside the body of a function component
View Code on pastebin: https://pastebin.com/jqhK9zee
import {
...
SET_FILTERS,
...
} from '../actions/lokale';
import Lokal from './../../models/lokal';
import useGetDistance from '../../handler/useGetDistance';
const initialState = {
...
filteredLokale: [],
...
};
const getDistanceHandler = useGetDistance();
const lokaleReducer = (state = initialState, action) => {
switch (action.type) {
...
case SET_FILTERS:
//TODO
const filters = action.filter;
const updatedFilteredLokale = state.lokale.filter((lokal) => {
let bool = true;
//Name
if (filters[0]) {
bool = lokal.name.toLowerCase().indexOf(filters[0].toLowerCase()) > -1;
}
//Category
if (bool && filters[1]) {
bool = lokal.category.toLowerCase().indexOf(filters[1].toLowerCase()) > -1;
}
if (bool && filters[2]) {
getDistanceHandler(region.latitude, region.longitude).then((distance) => {
bool = distance <= filters[2];
});
}
return bool;
});
return { ...state, filteredLokale: updatedFilteredLokale };
...
default:
return state;
}
};
export default lokaleReducer;
Upvotes: 4
Views: 5664