{"route":"/en-US-v0.14.1/reference/introspection/state/","title":"State","description":"Documentation for the State type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"state-and-markup","name":"State And Markup","children":[]},{"id":"state-in-typst","name":"State In Typst","children":[]},{"id":"time-travel","name":"Time Travel","children":[]},{"id":"caution","name":"Caution","children":[]},{"id":"constructor","name":"Constructor","children":[{"id":"constructor-key","name":"key","children":[]},{"id":"constructor-init","name":"init","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-get","name":"Get","children":[]},{"id":"definitions-at","name":"At","children":[{"id":"definitions-at-selector","name":"selector","children":[]}]},{"id":"definitions-final","name":"Final","children":[]},{"id":"definitions-update","name":"Update","children":[{"id":"definitions-update-update","name":"update","children":[]}]}]}],"body":{"kind":"type","content":{"name":"state","title":"State","keywords":[],"oneliner":"Manages stateful parts of your document.","details":"<p>Manages stateful parts of your document.</p>\n<p>Let's say you have some computations in your document and want to remember\nthe result of your last computation to use it in the next one. You might try\nsomething similar to the code below and expect it to output 10, 13, 26, and\n21. However this <strong>does not work</strong> in Typst. If you test this code, you will\nsee that Typst complains with the following error message: <em>Variables from\noutside the function are read-only and cannot be modified.</em></p>\n<pre><code><span class=\"typ-comment\">// This doesn&#39;t work!</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> star <span class=\"typ-op\">=</span> <span class=\"typ-num\">0</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span>expr<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  star <span class=\"typ-op\">=</span> <span class=\"typ-func\">eval</span><span class=\"typ-punct\">(</span>\n    expr<span class=\"typ-punct\">.</span><span class=\"typ-func\">replace</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-func\">str</span><span class=\"typ-punct\">(</span>star<span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">[</span>New value is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">star</span>.<span class=\"typ-punct\">]</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ - 5&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre>\n<h2 id=\"state-and-markup\">State and document markup</h2>\n<p>Why does it do that? Because, in general, this kind of computation with side\neffects is problematic in document markup and Typst is upfront about that.\nFor the results to make sense, the computation must proceed in the same\norder in which the results will be laid out in the document. In our simple\nexample, that's the case, but in general it might not be.</p>\n<p>Let's look at a slightly different, but similar kind of state: The heading\nnumbering. We want to increase the heading counter at each heading. Easy\nenough, right? Just add one. Well, it's not that simple. Consider the\nfollowing example:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">heading</span><span class=\"typ-punct\">(</span>numbering<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;1.&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">template</span><span class=\"typ-punct\">(</span>body<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-heading\">= Outline</span>\n  <span class=\"typ-escape\">...</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">body</span>\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span><span class=\"typ-punct\">:</span> <span class=\"typ-func\">template</span>\n\n<span class=\"typ-heading\">= Introduction</span>\n<span class=\"typ-escape\">...</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/382f18a61cf8fa6150847e8c9bd970c0.png\" alt=\"Preview\"></div></div>\n<p>Here, Typst first processes the body of the document after the show rule,\nsees the <code>Introduction</code> heading, then passes the resulting content to the\n<code>template</code> function and only then sees the <code>Outline</code>. Just counting up would\nnumber the <code>Introduction</code> with <code>1</code> and the <code>Outline</code> with <code>2</code>.</p>\n<h2 id=\"state-in-typst\">Managing state in Typst</h2>\n<p>So what do we do instead? We use Typst's state management system. Calling\nthe <code>state</code> function with an identifying string key and an optional initial\nvalue gives you a state value which exposes a few functions. The two most\nimportant ones are <code>get</code> and <code>update</code>:</p>\n<ul>\n<li>\n<p>The <a href=\"/en-US-v0.14.1/reference/introspection/state/#definitions-get\"><code>get</code></a> function retrieves the current value of the state.\nBecause the value can vary over the course of the document, it is a\n<em>contextual</em> function that can only be used when <a href=\"/en-US-v0.14.1/reference/context/\">context</a> is\navailable.</p>\n</li>\n<li>\n<p>The <a href=\"/en-US-v0.14.1/reference/introspection/state/#definitions-update\"><code>update</code></a> function modifies the state. You can give it\nany value. If given a non-function value, it sets the state to that value.\nIf given a function, that function receives the previous state and has to\nreturn the new state.</p>\n</li>\n</ul>\n<p>Our initial example would now look like this:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> star <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;star&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">0</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span>expr<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  star<span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>old <span class=\"typ-op\">=&gt;</span>\n    <span class=\"typ-func\">eval</span><span class=\"typ-punct\">(</span>expr<span class=\"typ-punct\">.</span><span class=\"typ-func\">replace</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-func\">str</span><span class=\"typ-punct\">(</span>old<span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">[</span>New value is <span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> star<span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>.<span class=\"typ-punct\">]</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ - 5&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/90558dbee771e808eca1f68cdf3a9180.png\" alt=\"Preview\"></div></div>\n<p>State managed by Typst is always updated in layout order, not in evaluation\norder. The <code>update</code> method returns content and its effect occurs at the\nposition where the returned content is inserted into the document.</p>\n<p>As a result, we can now also store some of the computations in variables,\nbut they still show the correct results:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-escape\">...</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> more <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ - 5&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">more</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/60d834289ed95a6cd0ead55e970b8e94.png\" alt=\"Preview\"></div></div>\n<p>This example is of course a bit silly, but in practice this is often exactly\nwhat you want! A good example are heading counters, which is why Typst's\n<a href=\"/en-US-v0.14.1/reference/introspection/counter/\">counting system</a> is very similar to its state system.</p>\n<h2 id=\"time-travel\">Time Travel</h2>\n<p>By using Typst's state management system you also get time travel\ncapabilities! We can find out what the value of the state will be at any\nposition in the document from anywhere else. In particular, the <code>at</code> method\ngives us the value of the state at any particular location and the <code>final</code>\nmethods gives us the value of the state at the end of the document.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-escape\">...</span>\n\nValue at <span class=\"typ-raw\">`&lt;here&gt;`</span> is\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> star<span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span><span class=\"typ-label\">&lt;here&gt;</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-strong\">*Here.*</span> <span class=\"typ-label\">&lt;here&gt;</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;⭐ - 5&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/51ba9ea8959d58ae8865d48419e2a96e.png\" alt=\"Preview\"></div></div>\n<h2 id=\"caution\">A word of caution</h2>\n<p>To resolve the values of all states, Typst evaluates parts of your code\nmultiple times. However, there is no guarantee that your state manipulation\ncan actually be completely resolved.</p>\n<p>For instance, if you generate state updates depending on the final value of\na state, the results might never converge. The example below illustrates\nthis. We initialize our state with <code>1</code> and then update it to its own final\nvalue plus 1. So it should be <code>2</code>, but then its final value is <code>2</code>, so it\nshould be <code>3</code>, and so on. This example displays a finite value because Typst\nsimply gives up after a few attempts.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-comment\">// This is bad!</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> x <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;key&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> x<span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>x<span class=\"typ-punct\">.</span><span class=\"typ-func\">final</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">+</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> x<span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/353191d880395aa736697d1ec67de606.png\" alt=\"Preview\"></div></div>\n<p>In general, you should try not to generate state updates from within context\nexpressions. If possible, try to express your updates as non-contextual\nvalues or functions that compute the new value from the previous value.\nSometimes, it cannot be helped, but in those cases it is up to you to ensure\nthat the result converges.</p>","constructor":{"path":[],"name":"state","title":"Construct","keywords":[],"oneliner":"Create a new state identified by a key.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Create a new state identified by a key.</p>"}],"self":false,"params":[{"name":"key","details":[{"kind":"html","content":"<p>The key that identifies this state.</p>\n<p>Any <a href=\"/en-US-v0.14.1/reference/introspection/state/#definitions-update\">updates</a> to the state will be identified with\nthe string key. If you construct multiple states with the same\n<code>key</code>, then updating any one will affect all of them.</p>"}],"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"init","details":[{"kind":"html","content":"<p>The initial value of the state.</p>\n<p>If you construct multiple states with the same <code>key</code> but different\n<code>init</code> values, they will each use their own initial value but share\nupdates. Specifically, the value of a state at some location in the\ndocument will be computed from that state's initial value and all\npreceding updates for the state's key.</p>"},{"kind":"example","content":{"body":"<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> banana <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;key&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-str\">&quot;🍌&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> broccoli <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;key&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-str\">&quot;🥦&quot;</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">banana</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>it <span class=\"typ-op\">=&gt;</span> it <span class=\"typ-op\">+</span> <span class=\"typ-str\">&quot;😋&quot;</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-marker\">-</span> <span class=\"typ-func\">#</span><span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;key&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-str\">&quot;🍎&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-marker\">-</span> <span class=\"typ-pol\">#</span><span class=\"typ-pol\">banana</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-marker\">-</span> <span class=\"typ-pol\">#</span><span class=\"typ-pol\">broccoli</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/9f5105246e9a16db3f675ecc36607343.png\" alt=\"Preview\"></div></div>","title":null}}],"types":["any"],"strings":[],"default":"<code><span class=\"typ-key\">none</span></code>","positional":true,"named":false,"required":false,"variadic":false,"settable":false}],"returns":["state"],"scope":[]},"scope":[{"path":["state"],"name":"get","title":"Get","keywords":[],"oneliner":"Retrieves the value of the state at the current location.","element":false,"contextual":true,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Retrieves the value of the state at the current location.</p>\n<p>This is equivalent to <code>state<span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">here</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span></code>.</p>"}],"self":true,"params":[],"returns":["any"],"scope":[]},{"path":["state"],"name":"at","title":"At","keywords":[],"oneliner":"Retrieves the value of the state at the given selector's unique match.","element":false,"contextual":true,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Retrieves the value of the state at the given selector's unique match.</p>\n<p>The <code>selector</code> must match exactly one element in the document. The most\nuseful kinds of selectors for this are <a href=\"/en-US-v0.14.1/reference/foundations/label/\">labels</a> and\n<a href=\"/en-US-v0.14.1/reference/introspection/location/\">locations</a>.</p>"}],"self":true,"params":[{"name":"selector","details":[{"kind":"html","content":"<p>The place at which the state's value should be retrieved.</p>"}],"types":["label","selector","location","function"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["any"],"scope":[]},{"path":["state"],"name":"final","title":"Final","keywords":[],"oneliner":"Retrieves the value of the state at the end of the document.","element":false,"contextual":true,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Retrieves the value of the state at the end of the document.</p>"}],"self":true,"params":[],"returns":["any"],"scope":[]},{"path":["state"],"name":"update","title":"Update","keywords":[],"oneliner":"Updates the value of the state.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Updates the value of the state.</p>\n<p>Returns an invisible piece of <a href=\"/en-US-v0.14.1/reference/foundations/content/\" title=\"content\">content</a> that must be inserted into the\ndocument to take effect. This invisible content tells Typst that the\nspecified update should take place wherever the content is inserted into\nthe document.</p>\n<p>State is a part of your document and runs like a thread embedded in the\ndocument content. The value of a state is the result of all state\nupdates that happened in the document up until that point.</p>\n<p>That's why <code>state.update</code> returns an invisible sliver of content that\nyou need to return and include in the document — a state update that is\nnot &quot;placed&quot; in the document does not happen, and &quot;when&quot; it happens is\ndetermined by where you place it. That's also why you need <a href=\"/en-US-v0.14.1/reference/context/\" title=\"context\">context</a> to\nread state: You need to use the current document position to know where\non the state's &quot;thread&quot; you are.</p>\n<p>Storing a state update in a variable (e.g.\n<code><span class=\"typ-key\">let</span> my-update <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;key&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>c <span class=\"typ-op\">=&gt;</span> c <span class=\"typ-op\">*</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">)</span></code>) will have no effect\nby itself. Only once you insert the variable <code><span class=\"typ-pol\">#</span><span class=\"typ-pol\">my-update</span></code> somewhere\ninto the document content, the update will take effect — at the position\nwhere it was inserted. You can also use <code><span class=\"typ-pol\">#</span><span class=\"typ-pol\">my-update</span></code> multiple times at\ndifferent positions. Then, the update will take effect multiple times as\nwell.</p>\n<p>In contrast to <a href=\"/en-US-v0.14.1/reference/introspection/state/#definitions-get\"><code>get</code></a>, <a href=\"/en-US-v0.14.1/reference/introspection/state/#definitions-at\"><code>at</code></a>, and\n<a href=\"/en-US-v0.14.1/reference/introspection/state/#definitions-final\"><code>final</code></a>, this function does not require <a href=\"/en-US-v0.14.1/reference/context/\" title=\"context\">context</a>. This\nis because, to create the state update, we do not need to know where in\nthe document we are. We only need this information to resolve the\nstate's value.</p>"}],"self":true,"params":[{"name":"update","details":[{"kind":"html","content":"<p>A value to update to or a function to update with.</p>\n<ul>\n<li>If given a non-function value, sets the state to that value.</li>\n<li>If given a function, that function receives the state's previous\nvalue and has to return the state's new value.</li>\n</ul>\n<p>When updating the state based on its previous value, you should\nprefer the function form instead of retrieving the previous value\nfrom the <a href=\"/en-US-v0.14.1/reference/context/\">context</a>. This allows the compiler to resolve\nthe final state efficiently, minimizing the number of\n<a href=\"/en-US-v0.14.1/reference/context/#compiler-iterations\">layout iterations</a> required.</p>\n<p>In the following example, <code>fill<span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>f <span class=\"typ-op\">=&gt;</span> <span class=\"typ-key\">not</span> f<span class=\"typ-punct\">)</span></code> will paint odd\n<a href=\"/en-US-v0.14.1/reference/model/list/#definitions-item\">items in the bullet list</a> as expected. However, if it's\nreplaced with <code><span class=\"typ-key\">context</span> fill<span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span><span class=\"typ-key\">not</span> fill<span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span></code>, then layout\nwill not converge within 5 attempts, as each update will take one\nadditional iteration to propagate.</p>"},{"kind":"example","content":{"body":"<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> fill <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;fill&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-key\">false</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> list<span class=\"typ-punct\">.</span><span class=\"typ-func\">item</span><span class=\"typ-punct\">:</span> it <span class=\"typ-op\">=&gt;</span> <span class=\"typ-punct\">{</span>\n  fill<span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>f <span class=\"typ-op\">=&gt;</span> <span class=\"typ-key\">not</span> f<span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">context</span> <span class=\"typ-punct\">{</span>\n    <span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>fill<span class=\"typ-punct\">:</span> fuchsia<span class=\"typ-punct\">)</span> <span class=\"typ-key\">if</span> fill<span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n    it\n  <span class=\"typ-punct\">}</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">5</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">split</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">map</span><span class=\"typ-punct\">(</span>list<span class=\"typ-punct\">.</span>item<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">join</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/396ec1571afb7936e381da6ef57923c.png\" alt=\"Preview\"></div></div>","title":null}}],"types":["any","function"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["content"],"scope":[]}]}}}