Skip to content
A library to provide access to local storage in Blazor applications
Branch: master
Clone or download
Latest commit ded1a34 Jun 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples Upgrade to preview 6 Jun 13, 2019
src/Blazored.LocalStorage Upgrade to preview 6 Jun 13, 2019
.gitignore Initial commit Jan 29, 2019
Blazored.LocalStorage.sln Completes upgrade to preview 4 Apr 19, 2019
LICENSE Initial commit Jan 29, 2019
README.md Update README.md Jun 13, 2019
azure-pipelines.yml Bumped SDK version in build script Jun 13, 2019

README.md

Blazored LocalStorage

A library to provide access to local storage in Blazor applications

Build Status

Nuget

Important Notice For Server-side Blazor Apps

There is currently an issue with Server-side Blazor apps (not Client-side Blazor). They are unable to import static assets from component libraries such as this one.

You can still use this package, however, you will need to manually add the JavaScript file to your Server-side Blazor projects wwwroot folder. Then you will need to reference it in your index.html.

Alternatively, there is a great package by Mister Magoo which offers a solution to this problem without having to manually copy files.

Installing

You can install from Nuget using the following command:

Install-Package Blazored.LocalStorage

Or via the Visual Studio package manger.

Setup

First, you will need to register local storage with the service collection in your startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    services.AddBlazoredLocalStorage();
}

Usage (Client-side Blazor)

To use Blazored.LocalStorage in client-side Blazor, inject the ILocalStorageService per the example below.

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@code {

    protected override async Task OnInitAsync()
    {
        await localStorage.SetItemAsync("name", "John Smith");
        var name = await localStorage.GetItemAsync<string>("name");
    }

}

With client-side Blazor you also have the option of a synchronous API, if your use case requires it. You can swap the ILocalStorageService for ISyncStorageService which allows you to avoid use of async/await. For either interface, the method names are the same.

@inject Blazored.LocalStorage.ISyncStorageService localStorage

@code {

    protected override void OnInit()
    {
        localStorage.SetItem("name", "John Smith");
        var name = localStorage.GetItem<string>("name");
    }

}

Usage (Server-side Blazor)

NOTE: Due to pre-rendering in server-side Blazor you can't perform any JS interop until the OnAfterRender lifecycle method.

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@functions {

    protected override async Task OnAfterRenderAsync()
    {
        await localStorage.SetItemAsync("name", "John Smith");
        var name = await localStorage.GetItemAsync<string>("name");
    }

}

The APIs available are:

  • asynchronous via ILocalStorageService:
    • SetItemAsync()
    • GetItemAsync()
    • RemoveItemAsync()
    • ClearAsync()
    • LengthAsync()
    • KeyAsync()
  • synchronous via ISyncStorageService:
    • SetItem()
    • GetItem()
    • RemoveItem()
    • Clear()
    • Length()
    • Key()

Note: Blazored.LocalStorage methods will handle the serialisation and de-serialisation of the data for you.

You can’t perform that action at this time.