Maintain State In Tauri Apps
This is how I'm maintaining state in the Rust portion of my Tauri apps. The method uses `tauri::State`` and `std::sync::Mutex`` to hold on to the values.
The example provides an input field and "Set State" button that stores a String. The "Get State" button retreives the String and outputs it on the page.
File: src/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script>
const {invoke} = window.__TAURI__.tauri;
async function setState() {
await invoke("set_state", {name: inputEl.value});
}
async function getState() {
outputEl.innerHTML = await invoke("get_state");
}
window.addEventListener("DOMContentLoaded", () => {
setStateButton.addEventListener('click', setState)
getStateButton.addEventListener('click', getState)
});
</script>
</head>
<body>
<div>
<button id="setStateButton">Set State</button>
<input id="inputEl" />
</div>
<div>
<button id="getStateButton">Get State</button>
<span id="outputEl"></span>
</div>
</body>
</html>
File: src-tauri/src/main.rs
use Mutex;
use State;