i try to create a store in a closure, here is my code
export const usePaginationData = (requestMethod) =>
create((set, get) => ({
data: [],
page: 1,
isLoading: false,
fetchMore: async () => {
set({ isLoading: true })
const newData = await requestMethod(get().page, 10);
set((state) => ({ data: [...state.data, ...newData.data] }));
},
}));
then in component
const {data,isLoading,fetchMore} = usePaginationData(mockPageDataRequest)()
console.log('isLoading',isLoading);
...
return <button onClick={fetchMore}>start request</button>
...
when button clicked, the fetchMore
be invoked, and isLoading
has been set to true
, however, the console log here always say isLoading false
.
why that?