GithubHelp home page GithubHelp logo

billhertzing / localstorage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blazored/localstorage

0.0 2.0 0.0 298 KB

A library to provide access to local storage in Blazor applications

License: MIT License

C# 86.63% TypeScript 13.37%

localstorage's Introduction

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

This is an example of using local storage in a .cshtml file

@inject Blazored.LocalStorage.ILocalStorageService localStorage

@functions {

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

}

If you are using Blazor (not Razor Components), you can choose to instead inject Blazored.LocalStorage.ISyncStorageService to opt into a synchronous API that allows you to avoid use of async/await. For either interface, the method names are the same.

@inject Blazored.LocalStorage.ISyncStorageService localStorage

@functions {

    protected override void OnInit()
    {
        localStorage.SetItem("name", "John Smith");
        var name = localStorage.GetItem<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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.