Replies: 2 comments
-
I think this ought to work. It doesn't. Maybe you can improve it. import { render } from "solid-js/web";
import { createStore, reconcile } from "solid-js/store";
import { createSignal } from "solid-js";
const deep = {
label: "second",
id: 2,
children: [
{
label: "third",
id: 3,
children: [
{
label: "fifth",
id: 5,
children: [],
},
{
label: "sixth",
id: 6,
children: [
{
label: "seventh",
id: 7,
children: [],
},
],
},
],
},
{
label: "fourth",
id: 4,
children: [],
},
],
};
function Deep() {
const [state, setState] = createStore(deep);
function findNode(id: number, node: any): any {
if (node.id == id) return node;
if (node.children) {
for (let child of node.children) {
const result = findNode(id, child);
if (result.id == id) return result;
}
}
return {};
}
function click() {
const node = findNode(7, state);
console.log({ click: node });
setState( node, {...node,text:"yay"});
console.log({ click: node});
}
return (
<>
<button type="button" onClick={click}>
click
</button>
<pre>{JSON.stringify(state, null, 2)}</pre>
</>
);
}
render(() => <Deep />, document.getElementById("app")!); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I love how I can access stores to write 1 or 2-level deep objects, but what happens if I need to go deeper?
how can I change a big nested object?
Let's say for example I want to change the label of "sixth" or add something to the array.
Beta Was this translation helpful? Give feedback.
All reactions