Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to access the results based on the query from the url #809

Closed
pranavp10 opened this issue Dec 9, 2020 · 3 comments
Closed

how to access the results based on the query from the url #809

pranavp10 opened this issue Dec 9, 2020 · 3 comments

Comments

@pranavp10
Copy link
Contributor

How to fetch the results based on the url similear below in version 3.0
https://xyz.com/search?q=react%20js

@pranavp10 pranavp10 reopened this Dec 9, 2020
@joemcelroy
Copy link
Member

Not easy to do yet, need to build in a way to support this but for time being

https://github.com/searchkit/searchkit/blob/next/packages/searchkit-client/src/searchkit.tsx#L152
this is where search is run by default when you land in the page.

One proposal i've got is allow to pass via a prop a function to run to build the initial search state

export function SearchkitProvider({ client, children, getInitialState }) {
  useEffect(() => {
    const state = getInitialState()
    client.setState(state)
    client.search()
  }, [])
  return <SearchkitContext.Provider value={client}>{children}</SearchkitContext.Provider>
}
<SearchkitProvider getInitialState={() => { 
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('q');
return {
  query: q
}
} />

as a searchkit supported way. For now if you dont want to PR a feature, you could hack it by recreating searchkitProvider and calling the client.setQuery(queryParam) before you trigger search

function CustomSearchkitProvider({ client, children }) {
  useEffect(() => {
    client.setQuery(queryParam)
    client.search()
  }, [])
  return <SearchkitContext.Provider value={client}>{children}</SearchkitContext.Provider>
}

@pranavp10
Copy link
Contributor Author

i am still not getting the context can you make example for the above one

joemcelroy added a commit that referenced this issue Dec 13, 2020
* build: add debugger for searchkit/client

* chore: update react types dep to 17

* docs: split guides and docs into seperate sections

* fix js error when run locally

* remove unneeded react types

* fix: fix error where input was uncontrolled

* feat(searchbar): if query is set, use it as default value

#809

* feat: allow query to be adjusted

#806

* bump to rc20
@joemcelroy
Copy link
Member

Hey so tried this out, you will need RC20 for it to work too.
https://github.com/searchkit/searchkit/blob/next/examples/next/pages/index.jsx#L9
So you set the query here when you pass the reference of SearchkitClient instance to the provider. Here ive hardcoded the query "heat" as an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants