Skip to content

Commit

Permalink
feat(query): added createQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Dec 5, 2019
1 parent b95cd4d commit 0dbd256
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import LocationState = History.LocationState

import memoizeOne from 'memoize-one'
import { stringify } from 'query-string'
import { createContext, useContext, useEffect, useMemo, useState } from 'react'
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState
} from 'react'
import { create, StoreApi, UseStore } from 'zustand'
// tslint:disable-next-line:no-submodule-imports
import shallow from 'zustand/shallow'
Expand Down Expand Up @@ -165,25 +172,34 @@ export const factoryParameters = <T = object>(
const values = currentState.values
const initialValues = currentState.initialValues

const createQuery = useCallback(
(values: T) => {
return createQueryObject(flatConfig, ns, values, initialValues)
},
[initialValues]
)

return useMemo(
() => ({
createQueryString: (customValues?: object) =>
stringify(
createQueryObject(
flatConfig,
ns,
customValues || values,
initialValues
)
),
createQuery,
createQueryString: (customValues?: T) =>
stringify(createQuery(customValues || values)),
initialValues,
pushState: (state: (state: T) => void) => pushState(ns, state),
replaceState: (state: (state: T) => void) => replaceState(ns, state),
resetPush: () => resetPush(ns),
resetReplace: () => resetReplace(ns),
values
}),
[values, initialValues, pushState, replaceState, resetPush, resetReplace]
[
values,
initialValues,
pushState,
replaceState,
resetPush,
resetReplace,
createQuery
]
)
}

Expand Down

0 comments on commit 0dbd256

Please sign in to comment.