Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Consider including HistoryState #1

@benglass

Description

@benglass
import { State } from '@symbiotic/green-state';

export class HistoryState extends State {
    constructor({ history }){
        const getQueryParams = () => {
            const { location } = history;
            const result = {};
            for (let [key, value] of new URLSearchParams(location.search).entries()){
                result[key] = value;
            }
            return result;
        }
        super({
            queryParams: getQueryParams()
        });
        history.listen(() => {
            this.setState({ queryParams : getQueryParams() });
        });
        this.setQueryParam = (key, value) => {
            const { queryParams } = this.get();
            const queryParamsCopy = { ...queryParams };
            if(value === null){
                delete queryParamsCopy[key];
            }
            else {
                queryParamsCopy[key] = encodeURIComponent(value);
            }
            const queryString = new URLSearchParams(queryParamsCopy).toString();
            history.push(`${history.location.pathname}?${queryString}`)
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions