How to set initial search on load

Hi I’m setting up instantsearch.js on my site and want to pull in a search query a la mysite.com/search/?q=dogs such that when the search page loads it loads the hits from ‘dogs’ in my index. I can’t find a clear example of how to do this that doesn’t seem very complicated. How do I get this to work?

After trying several approaches I got this structure to work:

    const search = instantsearch({
        indexName: 'MyIndex',
        searchClient,
        initialUiState: {
            'MyIndex: {
                query: searchQuery
            }
        }
    });

Hi Jordan!

You can indeed pass an initial state to your InstantSearch instance with initialUiState, but it won’t be persisted in the URL. If you want to be able to set a query from the URL, and persist it when it changes, you can pass routing: true in the options. Note that this will persist the entire search state, including the current page if you have pagination, the refinements, etc.

If you want finer control, I recommended following our routing guide.