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-valtio = "2"
Install the JavaScript package with your preferred package manager:
shell
npm install @tauri-store/valtio
shell
pnpm add @tauri-store/valtio
shell
deno add npm:@tauri-store/valtio
shell
bun add @tauri-store/valtio
shell
yarn add @tauri-store/valtio
Usage
- Enable the required permissions in your capabilities file:
json
{
"identifier": "valtio",
"windows": ["*"],
"permissions": ["valtio:default", "core:event:default"]
}
- Register the plugin with Tauri:
rust
tauri::Builder::default()
.plugin(tauri_plugin_valtio::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
- Create a store:
typescript
import { store } from '@tauri-store/valtio';
export const counterStore = store('counter', { counter: 0 });
export const increment = () => {
counterStore.state.counter++;
};
- Start it:
typescript
await counterStore.start();
TIP
Stores won't be saved nor synchronized until you call the start method.
- Use the store in your React components:
tsx
import { useSnapshot } from 'valtio';
import { counterStore, increment } from '@/stores/counter';
export default function MyComponent() {
// `state` is the actual valtio proxy.
const snap = useSnapshot(counterStore.state);
return (
<div>
<p>Counter: {snap.counter}</p>
<button type="button" onClick={increment}>
<span>Increment</span>
</button>
</div>
);
}