Query String

Synchronize component data properties with URL query parameters to create shareable, bookmarkable URLs that reflect your component's state. When users modify data properties, the URL automatically updates, and when they visit URLs with query parameters, your component initializes with those values.

Basic Usage

Create a search component that updates the URL as users type:

// wires/ArticleSearch.bx
class extends="cbwire.models.Component" {
    data = {
        "search": "",
        "category": "",
        "results": []
    };
    
    queryString = ["search", "category"];
    
    function onMount() {
        performSearch();
    }
    
    function onUpdate() {
        performSearch();
    }
    
    function performSearch() {
        if (data.search.len() > 2) {
            // Simulate search results
            data.results = [
                {"title": "Article 1", "category": "Tech"},
                {"title": "Article 2", "category": "News"}
            ];
        } else {
            data.results = [];
        }
    }
}
<!-- wires/articleSearch.bxm -->
<bx:output>
<div>
    <h1>Article Search</h1>
    
    <div class="search-form">
        <input wire:model="search" 
               type="search" 
               placeholder="Search articles...">
        
        <select wire:model="category">
            <option value="">All Categories</option>
            <option value="tech">Technology</option>
            <option value="news">News</option>
            <option value="sports">Sports</option>
        </select>
    </div>
    
    <div class="results">
        <h2>Results (#results.len()#)</h2>
        <bx:loop array="#results#" item="article">
            <div class="article">
                <h3>#article.title#</h3>
                <span class="category">#article.category#</span>
            </div>
        </bx:loop>
    </div>
</div>
</bx:output>

When users visit /articles?search=javascript&category=tech, the component automatically initializes with those values and performs the search.

Query string synchronization happens automatically when data properties change. You don't need to manually update the URL.

Last updated

Was this helpful?