r/solidjs • u/jml26 • Aug 20 '26
Help figuring out what's wrong with my `createOptimistic` example
I'm playing around with Solid 2.0's createOptimistic and action APIs to get a feel for them, but even with a basic example, I'm hitting a wall.
Here is my code that I'm using in the Solid Playground (version set to v2.0.0-rc.1 (next)). The idea is that when you click the button, an ID is generated and we simulate saving it to a slow, flaky server. While the value is being saved, show its optimistic value but display an asterisk next to it. If the API call succeeds, remove the asterisk; else revert the ID to what it was previously.
The issue I'm getting is that the ID always reverts to its initial value (in this case, the empty string) independent of whether the API call succeeds of fails.
Why is this? What do I need to change in my code to get it to work?
Apart from being a string rather than an array, my code feels practically identical to the example found at https://v2.solidjs.com/reference/solid-js/reactivity/create-optimistic#examples
AI has not been very helpful thus far.
import { createSignal, action, createOptimistic } from 'solid-js';
import { render } from '@solidjs/web';
type ApiSuccess = {
success: true,
data: string,
};
type ApiError = {
error: true,
data: string,
};
type ApiResponse = ApiSuccess | ApiError;
// echo back the data after a second's delay
// with a 20% change of erroring
function postToApi(data: string): Promise<ApiResponse> {
return new Promise(resolve => {
setTimeout(() => {
resolve(Math.random() < 0.2
? { data, error: true }
: { data, success: true }
);
}, 1000);
});
}
const [id, setId] = createOptimistic('');
const generateId = action(function* () {
const value = Math.random().toString(16).slice(2);
setId(value + ' (*)');
const res = yield postToApi(value);
if (!res.error) setId(value);
});
function App() {
return (
<>
<button onClick={() => generateId()}>
Generate ID
</button>
<pre>{JSON.stringify(id())}</pre>
</>
);
}
const root = document.getElementById('app')!;
render(() => <App/>, root);
2
u/jml26 Aug 20 '26
Thanks for your help and suggestions, guys! With your guidance and also trying some other things, I have a working solution. What I needed was:
So, I did have to have a separate
idsignal and wrap it. Fair enough. But also,createOptimisticin this case takes a thunk, not just a reference to theid. That was one part I was missing.Then,
generateIdlooks like this:And in the JSX
Well, that was a whirlwind, but I learned a lot in the process.
2
u/whoisarepo Aug 20 '26
I took a stab at this because 1. I’m a senior dev who’s unemployed, which makes me mad lol 2. because I’m building an open source project that heavily relies on solid JS 2.0 so i need to know it’s edge cases. Luckily, in your case, it seems like you simply failed to reconcile the different states of setTodo after pending with their ids. You need to synchronously call the setter again after the presume state is updated to satisfaction or failure. This is actually in the example but it’s subtle and feels weird cause it’s imperative while the caller is declarative. Hope this helps!
3
u/Monkeylordz88 Aug 20 '26
Optimistic state will always revert/recompute at the end of a transition, so any manual sets are ephemeral. What you really want is a regular signal for id, then an optimistic signal derived from that. In the action, you first set the optimistic id, then if that succeeds, then you assign the real id.
2
u/whoisarepo Aug 20 '26
Just an aside : I think in trying to solve the coloring problem it introduced an another ambiguity between computed and promised values, and it seems the idea is use generators to yield gracefully with optimistic fallbacks and cache. It’s a good idea but has a curve because of react trauma