Skip to content

Bind to nested reactive property #16267

Answered by brunnerh
SEUH asked this question in Q&A
Jun 30, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

If you make the binding interact with the source object and the source object is deeply stateful, this is possible.

Example:

<script>
	let { def, cell } = $props();

	let accessor = $derived.by(() => {
		const parts = def.accessorKey.split('.');
		const property = parts.at(-1);
		let o = cell.row.original;
		for (const key of parts.slice(0, -1))
			o = o[key];

		return {
			get value() { return o[property]; },
			set value(v) { o[property] = v; },
		}
	});
</script>

<input bind:value={accessor.value} />

Playground

In the case of shallow reactivity, the cell needs to be $bindable and the entire object has to be swapped out with an updated version, which is a bit more involved.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@SEUH
Comment options

@gterras
Comment options

Answer selected by SEUH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants