Getting started
Supported Tauri Version
This plugin requires Tauri 2.0 or later.
Install
Install the Rust crate by adding the following to your Cargo.toml
file:
toml
[dependencies]
tauri-plugin-zustand = "0.1"
Install the JavaScript package with your preferred package manager:
shell
npm install @tauri-store/zustand
shell
pnpm add @tauri-store/zustand
shell
deno add npm:@tauri-store/zustand
shell
bun add @tauri-store/zustand
shell
yarn add @tauri-store/zustand
Usage
- Enable the required permissions in your capabilities file:
json
{
"identifier": "zustand",
"windows": ["*"],
"permissions": ["zustand:default", "core:event:default"]
}
- Register the plugin with Tauri:
rust
tauri::Builder::default()
.plugin(tauri_plugin_zustand::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
- Create a store:
typescript
import { create } from 'zustand';
import { createTauriStore } from '@tauri-store/zustand';
type CounterStore = {
counter: number;
increment: () => void;
};
// A Zustand store, like any other.
export const useCounterStore = create<CounterStore>((set) => ({
counter: 0,
increment: () => set((state) => ({ counter: state.counter + 1 })),
}));
// A handle to the Tauri plugin.
// We will need this to start the store.
export const tauriHandler = createTauriStore('counter', useCounterStore);
- Start it:
typescript
await tauriHandler.start();
TIP
Stores won't be saved nor synchronized until you call the start method.
- Use the store in your React components:
tsx
import { useCounterStore } from '@/stores/counter';
export default function MyComponent() {
const counter = useCounterStore((state) => state.counter);
const increment = useCounterStore((state) => state.increment);
return (
<div>
<p>Counter: {counter}</p>
<button type="button" onClick={increment}>
<span>Increment</span>
</button>
</div>
);
}