@virtualstate/examples
Version:
94 lines • 3.75 kB
JavaScript
import { useState, useEffect, useContext, useRef } from "./source.interface.js";
import { View, Text, Source } from "./source.tokens.js";
import { h, createFragment } from "@virtualstate/fringe";
import { EngineURL, EngineURLSymbol } from "./source.engine.js";
import { SourceURLSymbol } from "./source.transform.js";
const context = useContext();
const [state, setState] = useState({
currentThing: 0
});
useEffect(() => {
console.log("On each second", state.currentThing, context.globalThing);
}, new Date().getSeconds());
useEffect(async () => {
console.log("On something else", state.currentThing, context.globalThing);
setState({
currentThing: 1
});
}, Math.random() < 0.1);
const previousStateThing = useRef(state.currentThing);
useEffect(() => {
console.log("State thing changed from", previousStateThing.current, "to", state.currentThing);
previousStateThing.current = state.currentThing;
if (state.currentThing > 10) {
WebDynamic = (h("div", null, "Did ten things!!"));
NativeDynamic = (h(Text, null, "Did ten things!!"));
}
}, state.currentThing);
const previousGlobalThing = useRef(context.globalThing);
useEffect(() => {
console.log("Global thing changed from", previousGlobalThing.current, "to", context.globalThing);
previousGlobalThing.current = context.globalThing;
}, context.globalThing);
export const Web = (h("div", null,
h("h1", null, "Global Thing:"),
h("span", null, previousGlobalThing.current),
h("h1", null, "State Thing:"),
h("span", null, state.currentThing)));
export const Native = (h(View, null,
h(Text, null, "Global Thing:"),
h(Text, null, previousGlobalThing.current),
h(Text, null, "State Thing:"),
h(Text, null, state.currentThing)));
export let WebDynamic = (h("div", null,
h("h1", null, "Global Thing:"),
h("span", null, previousGlobalThing.current),
h("h1", null, "State Thing:"),
h("span", null, state.currentThing)));
export let NativeDynamic = (h(View, null,
h(Text, null, "Global Thing:"),
h(Text, null, previousGlobalThing.current),
h(Text, null, "State Thing:"),
h(Text, null, state.currentThing)));
export async function SourceComponent({ componentThing }) {
const [componentState, setComponentState] = useState({
currentThing: 0
});
useEffect(async () => {
if (state.currentThing > 10) {
setComponentState({
currentThing: 1
});
}
}, state.currentThing);
return (h(createFragment, null,
h(Source, null,
h("div", null,
h("h1", null, "Global Thing:"),
h("span", null, previousGlobalThing.current),
h("h1", null, "State Thing:"),
h("span", null, state.currentThing),
h("h1", null, "Component Thing:"),
h("span", null, componentThing),
h("h1", null, "Component State Thing:"),
h("span", null, componentState.currentThing))),
h(Source, null,
h(View, null,
h(Text, null, "Global Thing:"),
h(Text, null, previousGlobalThing.current),
h(Text, null, "State Thing:"),
h(Text, null, state.currentThing),
h(Text, null, "Component Thing:"),
h(Text, null, componentThing)))));
}
export const _CT0001_CompileTransform = (h(createFragment, null,
h(Source, null, Web),
h(Source, null, Native),
h(Source, null, WebDynamic)));
export const _CT0001_URL = import.meta.url;
export const _CT0001_Info = {
[EngineURLSymbol]: EngineURL,
[SourceURLSymbol]: import.meta.url,
...context
};
//# sourceMappingURL=source.js.map