Comments (9)
aha, it's a bug, but not what you think because the generated code looks the same, and when you switch the order of the lines it's still happening.
We'll need to check if this still happens in v2.
As for the memo, that way the object only gets created once and you can put it in a context
from qwik.
On my phone, but I see that you define a getter, that doesn't work, all state objects must be plain objects and all functions must be qrls.
Also, you can use https://qwik.dev/playground for testing this, that way you can easily inspect optimizer output and use different versions
from qwik.
Yeah, I get your point and all the requirements you mentioned are met. But the point is that getter doesnβt work if the variable is called in a specific way. It seems like the name of a variable plays important role whether getter works or not. Seems like an issueπ€·ββοΈ
from qwik.
Hmm, I put your code in the playground and renamed it to useToggle and it just works π€
(It also puts Foo outside the html which is a bug but which I think is already fixed in v2 so that's not relevant here)
from qwik.
And here's a working example of the state machine, the getter was interfering with the reactivity. Note the little trick of embedding it into a helper object so the qrls can reference "self". This state machine object is self-contained.
import { component$, QRL, useSignal, $, ValueOrPromise, Signal } from '@builder.io/qwik'
export type Toggle<State> = {
toggle: QRL<() => ValueOrPromise<State>>;
getState: QRL<() => State>
setState: QRL<(newState: State) => ValueOrPromise<State>>;
state: Signal<number>
states: State[]
};
export const useToggle = <State,>(states: State[]): Toggle<State> => {
const state = useSignal(0);
const memo = useSignal(() => {
// needed to reference self from qrls
const store: { self: Toggle<State> } = {} as any
store.self = {
state, states,
toggle: $(() => {
const { state, states } = store.self
console.log("=>(useToggle.ts:16) state.value", state.value);
state.value = (state.value + 1) % states.length;
console.log("=>(useToggle.ts:16) state.value", state.value);
return store.self.getState();
}),
getState: $(() => store.self.states[store.self.state.value]),
setState: $((newState: State) => {
const { state, states } = store.self
state.value =
states.indexOf(newState) === -1
? state.value
: states.indexOf(newState);
return store.self.getState();
}),
} as Toggle<State>
return store.self
})
return memo.value
};
export default component$(() => {
const toggle = useToggle(["Foo", "Bar"]);
return (
<>
<span>{toggle.getState()}</span>
<button onClick$={() => toggle.toggle()}>Hi</button>
</>
)
});
from qwik.
Hmm, I put your code in the playground and renamed it to useToggle and it just works π€
(It also puts Foo outside the html which is a bug but which I think is already fixed in v2 so that's not relevant here)
Thank you for putting my example into playground. Actually, it perfectly reproduces the bug I described: please pay attention to the text content of <span>{toggle.currentState}</span> with useToggle and createToggle. For your convenience I modified slightly your playground:
from qwik.
const memo = useSignal(() => { // needed to reference self from qrls const store: { self: Toggle<State> } = {} as any store.self = { state, states, toggle: $(() => { const { state, states } = store.self console.log("=>(useToggle.ts:16) state.value", state.value); state.value = (state.value + 1) % states.length; console.log("=>(useToggle.ts:16) state.value", state.value); return store.self.getState(); }), getState: $(() => store.self.states[store.self.state.value]), setState: $((newState: State) => { const { state, states } = store.self state.value = states.indexOf(newState) === -1 ? state.value : states.indexOf(newState); return store.self.getState(); }), } as Toggle<State> return store.self }) return memo.value
Yeah, I got your idea about self-containing state maching, that's an interesting solution, thank you! But what's the need of storing state machine in signal? What's the point of memo variable? Why not just useToggle shoud return store.self? It seems to also be a working solution
from qwik.
oh and actually, since you're using a getter I don't think we should dig much further into this issue, since those aren't supported. They look like plain values to the serializer and therefore reactivity is broken.
So I propose we close this issue.
from qwik.
oh and actually, since you're using a getter I don't think we should dig much further into this issue, since those aren't supported. They look like plain values to the serializer and therefore reactivity is broken.
But why then my approach is still working when using your solution? Could you please explain what you mean by reactivity? Isn't getter in your example reactive?
As someone from the Vue world, I find getters like this highly convenient for code reusability and composability. It would be really upsetting if they would not be supported by Qwik
from qwik.
Related Issues (20)
- [π] replace p element in starters HOT 2
- [β ] fixes for all "Error: Missing Qwik City Env Data" HOT 2
- [π] Non-deterministic builds
- [π] onLoad$ img cookbook
- [π] SSG ignoring the index page when trailingSlash is false HOT 3
- [π] Disable Link prefetch true by default HOT 2
- [π] Sidebar Issue HOT 2
- [β¨] How to distinguish between link preload and page visit HOT 1
- [π] Qwik dev server fails when using mkcert/https because of http2 headers HOT 1
- [π] Copy the current Qwik docs homepage
- [β¨] Throw server$ errors with a specific status code HOT 4
- [π] Attribute re-render on spread in polymorphism does not work correctly HOT 1
- [π] Route is broken with deep nesting HOT 2
- [β¨] Automatic simple 404 responses for file requests HOT 7
- [β¨] Create a unique file name for each version of service-worker.js HOT 16
- [π] Error Boundary Issue
- [π] Qwik server$ throws 500 error [Firebase deployment] HOT 2
- [π] Qwik Auth is not getting published HOT 2
- Link is broken [π] HOT 6
- [π] check all links in docs to make sure they work
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qwik.